I have been trying to get the scripts below to export to a csv file. I am pretty new to Powershell. Any help is greatly appreciated.
# get-content H:\src\win\hostlist.txt | % {
# #("dsp021","dsp075") | % {
#("dsp075") | % {
write "Name, Manufacturer, Model"
gwmi win32_computersystem -ComputerName $_ | select -Property Manufacturer, Model, Name
gwmi win32_computersystemproduct -ComputerName $_ | select uuid
write "mac addresses:"
gwmi win32_networkadapter -ComputerName $_ | ? { $_.MACAddress -match ":" } | select MACAddress
write "Solarflare network IDs"
gwmi win32_pnpentity -ComputerName $_ | ? { $_.Manufacturer -match "Solar"} | Select Name,DeviceID
write "LSI Disk controller IDs"
gwmi win32_pnpentity -ComputerName $_| ? { $_.Manufacturer -match "LSI"} | Select Name,DeviceID
} |Format-list
Thank you!
I modified your code so it can be exportable into a CSV file.
I am not sure if this is the format you wanted but it should give you an idea.
#("computername") | % {
gwmi win32_computersystem -ComputerName $_ | select -Property Manufacturer, Model, Name | ConvertTo-Csv -NoTypeInformation
gwmi win32_computersystemproduct -ComputerName $_ | select uuid | ConvertTo-Csv -NoTypeInformation
#write "mac addresses:"
gwmi win32_networkadapter -ComputerName $_ | ? { $_.MACAddress -match ":" } | select #{Name="Mac Addresses";Expression={$_."MACAddress"}} | ConvertTo-Csv -NoTypeInformation
#write "Solarflare network IDs"
gwmi win32_pnpentity -ComputerName $_ | ? { $_.Manufacturer -match "Solar"} | Select #{Name="Solarflare network Names";Expression={$_."Name"}} ,#{Name="Device IDs";Expression={$_."DeviceID"}} | ConvertTo-Csv -NoTypeInformation
#write "LSI Disk controller IDs"
gwmi win32_pnpentity -ComputerName $_| ? { $_.Manufacturer -match "LSI"} | Select #{Name="LSI Disk controller Names";Expression={$_."Name"}} ,#{Name="Device IDs";Expression={$_."DeviceID"}} |ConvertTo-Csv -NoTypeInformation
} | out-file C:\works.csv
The script convert all commands out-put into CSV before exporting everything into a file.
Related
Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content "C:\Temp\Servers.txt") | SELECT-Object PSComputerName, #{Name="Memory (RAM in GB)";Expression={[Math]::Round($_.TotalVisibleMemorySize/1024/1024)}} | Format-Table
Get-WmiObject -Class Win32_logicaldisk -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object PSComputerName, DriveType, DeviceID, VolumeName, #{Name="Size";Expression={[math]::ceiling($_.Size /1GB)}} , #{Name="FreeSpace";Expression={[math]::ceiling($_.FreeSpace /1GB)}}, Compressed | where DriveType -eq 3 | Format-Table
Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content "C:\Temp\Servers.txt")| Select-Object PSComputerName, BuildNumber, BuildType, Caption, CodeSet, OSArchitecture, SystemDrive, TotalVisibleMemorySize, Version | Format-Table
Get-WmiObject -Class win32_product -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object Name, Version, Vendor, InstallDate | Format-Table
Get-WmiObject -Class Win32_Service -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object PSComputerName, DisplayName, StartName, PathName, StartMode| where DisplayName -Like "*xyz*" |Format-Table
I have till now managed to piece together the above to get the information I need from serveral servers, however now I want to format it so that I can collate information for each server in a format that I can display
for eg.
Server : ABC
RAM : 64 GB
Number of Processors : 8
Disk :
Table of disk Sizes Etc
Any pointers would be appreciated
With all these properties, you would get a nested object array, which probably is easiest to view in JSON format.
I have changed all Get-WmiObject into the newer and faster Get-CimInstance cmdlets below
$result = Get-Content "C:\Temp\Servers.txt" | ForEach-Object {
# create an ordered hashtable to store the results for each server
$pcinfo = [ordered]#{}
# System info
$data = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $_
$pcinfo['Computer'] = $data.PSComputerName
$pcinfo['Memory (RAM in GB)'] = '{0:N2}' -f ($data.TotalPhysicalMemory / 1GB)
# OS info
$data = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $_
$pcinfo['BuildNumber'] = $data.BuildNumber
$pcinfo['BuildType'] = $data.BuildType
$pcinfo['Caption'] = $data.Caption
$pcinfo['CodeSet'] = $data.CodeSet
$pcinfo['OSArchitecture'] = $data.OSArchitecture
$pcinfo['SystemDrive'] = $data.SystemDrive
$pcinfo['TotalVisibleMemorySize'] = $data.TotalVisibleMemorySize
$pcinfo['Version'] = $data.Version
# Product info (array of objects)
$pcinfo['Products'] = Get-CimInstance -ClassName Win32_Product -ComputerName $_ |
Select-Object Name, Version, Vendor, InstallDate
# Local fixed disk info (array of objects)
$pcinfo['FixedDrives'] = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $_ -Filter 'DriveType=3' |
Sort-Object DeviceID |
Select-Object DriveType, DeviceID, VolumeName,
#{Name="Size";Expression={"{0:N2} GB" -f ($_.Size / 1GB)}},
#{Name="FreeSpace";Expression={"{0:N2} GB" -f ($_.FreeSpace / 1GB)}},
Compressed
# Services info (array of objects)
$pcinfo['Services'] = Get-CimInstance -ClassName Win32_Service -ComputerName $_ |
Where-Object { $_.DisplayName -like '*Adobe*' } |
Select-Object DisplayName, StartName, PathName, StartMode
# convert the hashtable to PSObject and output
[PsCustomObject]$pcinfo
}
# output the whole structure as JSON for easier reading and optionally save it to file
$result | ConvertTo-Json -Depth 3 # | Set-Content -Path 'Path\To\Output.json' -Force
I would like to return output for below in rows and cols style not one after another.
$path = "$([Environment]::GetFolderPath("Desktop"))\Installed Hotfixes Info.txt";
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . | fl * | Format-Table -AutoSize |fl > $path; notepad $path;
Remove the Format-List (fl):
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . |
Format-Table -AutoSize |
Out-File $path
Better yet, export the output as CSV:
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . |
Export-Csv $path -NoType
I wrote a small script to get some basic information off a few remote servers. But my output it is a bit odd. I believe my issue is with my $DRIVE function.
Code:
$serversList = 'svr01.xxx.com',
'svr03.xxx.com',
'svr05.xxx.com',
'svr06.xxx.com',
'svr08.xxx.com'
#End of Server List
Write-Output "Start of Hal0 `n";
ForEach ($server in $serversList) {
$OS = (Get-WmiObject -class Win32_OperatingSystem).Caption
$SYSNAME = (Get-WmiObject -class Win32_LogicalDisk -ComputerName $server).SystemName
$DRIVE = {
Get-WmiObject -Class Win32_LogicalDisk -ComputerName $server |
Where-Object {$_.DriveType -eq 3} |
Select-Object DeviceID, Description,`
#{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, `
#{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}} |
FT -AutoSize
}
$server + ' | ' + $SYSNAME + ' | ' + $OS + ' | '
}
Write-Output "`n End of Hal0";
Results:
Start of Hal0
svr01.xxx.com | SVR01 SVR01 SVR01 SVR01 | Mic
rosoft Windows 8.1 Enterprise |
svr03.xxx.com | SVR03 SVR03 SVR03 SVR03 | Mic
rosoft Windows 8.1 Enterprise |
svr05.xxx.com | SVR05 SVR05 SVR05 SVR05 | Mic
rosoft Windows 8.1 Enterprise |
svr06.xxx.com | SVR06 SVR06 SVR06 SVR06 | Mic
rosoft Windows 8.1 Enterprise |
svr08.xxx.com | SVR08 SVR08 SVR08 SVR08 | Mic
rosoft Windows 8.1 Enterprise |
End of Hal0
What I was hoping my result would be a clean System Full Name, System Short Name, OS, Hard Drive Free/Used Space for each of the 5 servers.
svr08.xxx.com | svr08 | Windows 8 | C: 1111 MB Free/500 MB Used, E: 11 MB Free/10 MB Used.
You never output $DRIVE anywhere, and the expression for $DRIVE shouldn't be in a scriptblock in the first place. The computer name is repeated several times, because you get the SystemName property for each logical disk object. Also, $OS gets the OS name for the local computer, not the remote computer.
Change your code to something like this:
$serversList | ForEach-Object {
$os = Get-WmiObject -Class Win32_OperatingSystem -Computer $_
$disks = Get-WmiObject -Class Win32_LogicalDisk -Computer $_ |
Where-Object {$_.DriveType -eq 3} |
ForEach-Object {
'{0} {1:D} MB Free/{2:D} MB Used' -f $_.DeviceID,
[int]($_.FreeSpace/1GB), [int]($_.Size/1GB)
}
New-Object -Type PSCustomObject -Property #{
'FQDN' = $_
'ServerName' = $os.PSComputerName
'OperatingSystem' = $os.Caption
'Disks' = $disks -join ', '
}
} | Export-Csv 'C:\output.csv' -Delimiter '|' -NoType
If you want the output echoed instead of written to a file use ConvertTo-Csv instead of Export-Csv.
Addendum: If you want to import the output file in a database use commas as the field separators for the CSV and join the disk information with some other character:
$serversList | ForEach-Object {
...
New-Object -Type PSCustomObject -Property #{
'FQDN' = $_
'ServerName' = $os.PSComputerName
'OperatingSystem' = $os.Caption
'Disks' = $disks -join '|'
}
} | Export-Csv 'C:\output.csv' -NoType
I am trying to provide a list of servers in our domain that lists 2003 and 2008/r2 servers. Along with this information i would like to give the current status of their individual C Drive "free space" & "size of the disk". The script below runs fine and prints a list of all the correct operating systems - But...
The free space and Size are all identical..it gives the first servers drive status and replicates this untill the script finishes. for example the script prints:
serverName1 Windows server 2003 standard deviceid=c freespace=40gb size=12gb
serverName2 Windows server 2008r2 standard deviceid=c freespace=40gb size=12gb
....
serverName100 ..... freespace=40gb size=12gb
Import-Module activedirectory
$2008LogPath = "e:/2008servers.txt"
$2003LogPath = "e:/2003servers.txt"
$servers = get-adcomputer -Filter 'ObjectClass -eq "Computer"' -properties "OperatingSystem"
foreach ($server in $servers) {
if($server.OperatingSystem -match "Windows Server 2008") {
Get-WmiObject win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2008LogPath }
elseif($server.operatingsystem -match "Windows Server 2003") {
Get-WmiObject win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2003LogPath }
}
You'll need to use the -ComputerName parameter of the Get-WmiObject cmdlet, to retrieve information from those remote computers. If you don't specify the -ComputerName parameter, then you're retrieving WMI data from the local computer.
To fix this, change your foreach loop to look like the following:
foreach ($server in $servers) {
if($server.OperatingSystem -match "Windows Server 2008") {
Get-WmiObject -ComputerName $Server.Name -Class win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2008LogPath }
elseif($server.operatingsystem -match "Windows Server 2003") {
Get-WmiObject -ComputerName $Server.Name -Class win32_logicaldisk | Where-Object {$_.deviceid -match "C"} |
ft $server.name, $server.operatingsystem, deviceid, freespace, size -AutoSize }#Out-File -Append $2003LogPath }
}
I can only get the command to return the services on the first computer in the text file.
Is there a better way than for-each for this task?
Get-Service *vault* -ComputerName (Get-Content c:\users\sean\desktop\js.txt) | select name,status,machinename | sort machinename | format-table -autosize
Try it without the get-content. Try this:
Get-Service *vault* -ComputerName c:\users\sean\desktop\js.txt | select name,status,machinename | sort machinename | format-table -autosize
If that doesn't work, then try:
$Computers = Get-Content c:\users\sean\desktop\js.txt
Get-Service *vault* -computername $Computers | Select name,status,machinename |sort machinename |format-table -autosize
If you are eager for a one-liner then try this:
Get-Content c:\users\sean\desktop\js.txt | Get-Service *vault* | Select name,status,machinename |sort machinename |format-table -autosize
I would try the top one first. I would test, but I don't have access to anything I can do a proper test right now.
$Computers = get-content .\desktop\test.txt
$Service = "Vault"
foreach ($computer in $computers) {
$computer
$Servicestatus = get-service -name $Service -ComputerName $computer
}
$Servicestatus | select-object Name,Status,MachineName | format-table -Autosize
This works for me, it gives me each of the computers in the text file, and it looks for the service.
This is what I use. I get the list of computers from an OU in AD.
Import-Module ActiveDirectory
$ou = "OU=Servers,DC=Domain,DC=com"
$servers = Get-ADComputer -Filter * -SearchBase $ou | select-object -expandproperty name
Foreach ($server in $servers){
$Data = Get-Service -ServiceName *IIS*,*TomCat*,*httpd* -ComputerName $server | select machinename,name | sort machinename | format-table -AutoSize
Write($Data) | Out-File .\WebServices.txt -Append
}
$servers = Get-Content .\servers.txt
Foreach ($server in $servers) {
"$server"
Get-Service -ComputerName $Server -name -like "*vault*"
"-------------------"
}
Following a memory limitation limit with older versions of PowerShell, I was required to refresh my code:
Old code:
gwmi win32_service -computer $allcomputers | Select-Object __SERVER,Name,state,startmode,StartName
New code:
`$servers = Get-Content "computers.txt"
Foreach ($server in $servers) {
Get-WmiObject -Class WIN32_service -ComputerName $server |
Select-Object __SERVER,Name,state,startmode,StartName |
Export-Csv -path "Report.CSV" -NoTypeInformation -Append
}`
This is how you can get list of all services in your AD domain:
Get-ADComputer -Filter {OperatingSystem -Like “Windows 10*”} | ForEach-Object {Get-WmiObject -Class Win32_Service -Computer $_.Name}
More useful examples on this (get list of services for all computer listed in a text file, etc.):
https://www.action1.com/kb/list_of_services_on_remote_computer.html
Get-Service -ComputerName ... has a bug in PowerShell 2.0 that only returns the first computer. This is fixed in newer versions so if you upgrade to PowerShell 3.0 or newer, your original code will work fine.
As a workaround, use a foreach-loop to run Get-Service once for each computer:
Get-Content c:\users\sean\desktop\js.txt |
ForEach-Object { Get-Service -Name *vault* -ComputerName $_ } |
Select-Object -Property Name, Status, MachineName |
Sort-Object -Property MachineName |
Format-Table -AutoSize
Nick's solution totally doesn't work for me. I ended up writing a quick and dirty one that works:
$servers = Get-Content .\servers.txt
Foreach ($server in $servers) {
"$server"
Get-Service *vault*
"-------------------"
}