A recent post on the powershell.com site asked about running a Windows command (batch) file through PowerShell against remote machines. The batch file was expected to produce a number of files of data in a folder structure based on computer name. This makes it difficult as we could in theory run the batch file through remoting (not actually tried it) but writing to a file share isn’t easy.
I decide to convert the batch file to PowerShell. The first part uses a number of basic Windows commands to generate data. This function supplies exactly the same functionality
function get-basicdata{ [CmdletBinding()] param ( [string]$computer="localhost" ) BEGIN{}#begin PROCESS{ Write-Verbose "Get Operating System" $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer Write-Verbose "Get Computer System" $comp = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer Write-Verbose "Get IP Address" $ip = Test-Connection -ComputerName $computer -Count 1 Write-Verbose "Read registry entry" $HKLM = 2147483650 #HKEY_LOCAL_MACHINE $reg = [wmiclass]"\\$computer\root\default:StdRegprov" $key = "SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\rdp-tcp" $value = "MinEncryptionLevel" $minlvl = $reg.GetDwordValue($HKLM, $key, $value) ## REG_DWORD Write-Verbose "Create Object" $obj = New-Object -TypeName PSObject $obj | Add-Member -MemberType NoteProperty -Name OperatingSystem -Value $($os.Caption) -PassThru | Add-Member -MemberType NoteProperty -Name ServicePack -Value $($os.CSDVersion) -PassThru | Add-Member -MemberType NoteProperty -Name Version -Value $($os.Version) -PassThru | Add-Member -MemberType NoteProperty -Name Domain -Value $($comp.Domain) -PassThru | Add-Member -MemberType NoteProperty -Name Name -Value $($comp.Name) -PassThru | Add-Member -MemberType NoteProperty -Name IPv4Address -Value $($ip.IPV4Address.IPAddressToString) -PassThru | Add-Member -MemberType NoteProperty -Name MinEncrypt -Value $($minlvl.uValue) $obj }#process END{}#end }
All we do is make a few calls to WMI classes and we get the results into an object as shown.
To produce a file use
The contents look like this
OperatingSystem : Microsoft Windows 7 Ultimate
ServicePack : Service Pack 1
Version : 6.1.7601
Domain : WORKGROUP
Name : RSLAPTOP01
IPv4Address : 127.0.0.1
MinEncrypt : 2
That covers the basic data. Need to look at services and TCP ports next
By: Tony on July 1, 2011 at 4:13 pm
Where is the file produce at? When executing this it returns to the prompt, no results. ?
By: RichardSiddaway on July 2, 2011 at 3:45 pm
Sorry missed a line out.
It should read
To produce a file use
get-basicdata | outfile c:\scripts\basicdata.txt
By: Ben W on July 2, 2011 at 10:20 pm
I have never seen Add-Member pipelined like that before, interesting.
By: RichardSiddaway on July 3, 2011 at 11:22 am
see
http://msmvps.com/blogs/richardsiddaway/archive/2011/07/03/using-add-member.aspx
for more on using add-member