I currently have the following script that works well for gathering a list of installed programs from a list of remote computers.
$PCListOld = Get-Content F:\PCList-Old.txt
ForEach ($PC in $PCListOld)
{
$AppList = Get-WmiObject -Computer $PC Win32_Product | Sort-Object Name
$AppList | Export-CSV C:\~Scripts\AppLists\$PC.csv
}
However, I really only need the Name property in $AppList, but if I simply pipe $AppList.Name to Export-CSV, I don't get the same output in the csv as I would have on the screen. Can someone give me some advice on how I should edit this so I can just get the Name value exported to the csv file?
Thanks in advance for the help.
Restrict the result properties to just Name via Select-Object:
foreach ($PC in $PCListOld) {
Get-WmiObject -Computer $PC Win32_Product |
Sort-Object Name |
Select-Object Name |
Export-Csv C:\~Scripts\AppLists\$PC.csv
}
Related
I have a script that I am trying to collect drive letters from a list of servers (as well as used space and free space) and then gridview the results out.
$servers = Get-Content "path.txt"
foreach ($server in $servers) {
Invoke-Command -ComputerName $server {Get-PSDrive | Where {$_.Free -gt 0}}
Select-Object -InputObject usedspace,freespace,root,pscomputername |
Sort-Object root -Descending | Out-Gridview
}
I can get it to display the drive information for each server on the list but gridview does not work. I have tried moving the brackets around (before and after gridview) as well as piping elements but have had no luck.
Can anyone advise me as to what I am doing wrong? I feel like it is something simple but all of the examples I am finding online do not use the foreach command which I think has to do with throwing it off.
Your Select-Object is missing pipeline input - pipe the Invoke-Command call's output to it.
Instead of -InputObject, use -Property:
Note: -InputObject is the parameter that facilitates pipeline input, and is usually not meant to be used directly.
As with Sort-Object, -Property is the first positional parameter, so you may omit -Property in the call below.
foreach ($server in Get-Content "path.txt") {
Invoke-Command -ComputerName $server { Get-PSDrive | Where { $_.Free -gt 0 } } |
Select-Object -Property usedspace, freespace, root, pscomputername |
Sort-Object root -Descending |
Out-Gridview
}
Also note that -ComputerName can accept an array of computer names, which are then queried in parallel, so if you want to query all computers and then call Out-GridView only once, for the results from all targeted computers:
Invoke-Command -ComputerName (Get-Content "path.txt") {
Get-PSDrive | Where Free -gt 0
} |
Select-Object -Property usedspace, freespace, root, pscomputername |
Sort-Object root -Descending |
Out-Gridview
To group the results by target computer, use
Sort-Object pscomputername, root -Descending
If you'd rather stick with your sequential, target-one-server-at-a-time approach, change from a foreach statement - which cannot be used directly as pipeline input - to a ForEach-Object call, which allows you to pipe to a single Out-GridView call:
Get-Content "path.txt" |
ForEach-Object {
Invoke-Command -ComputerName $_ { Get-PSDrive | Where Free -gt 0 }
} |
Select-Object -Property usedspace, freespace, root, pscomputername |
Sort-Object root -Descending |
Out-Gridview
I'm collecting information using a WMI query. I want to send this information to a CSV file including the machine name. I can send the information but I'm not able to include machine name with it.
$PasswordState = Get-WmiObject -Class Lenovo_BiosPasswordSettings -Namespace root\wmi |
Select -Expand PasswordState |
Select-Object -Last 1 |
Out-File -FilePath '\\server\share\Bios_Password_Status.csv' -Append -Encoding Unicode
I need this to create a CSV file with two items:
Data returned from WMI query
Machine name
Does this give you what you want, it will store the result in an array called $Output. You can then use $Output | Out-File -Append to add it to a file or something.
$Result = Get-WmiObject -Class Lenovo_BiosPasswordSettings -Namespace root\wmi | Select -Property PSComputerName,PasswordState
$Output += $Result | Format-Table -HideTableHeaders
$ErrorActionPreference = 'SilentlyContinue'
$ComputerName =Get-ADComputer -Filter {(Name -like "*")} -SearchBase "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM" | Select-Object -ExpandProperty Name
$results = #{}
ForEach ($computer in $ComputerName) {
$Results += Get-NetAdapter -CimSession $ComputerName | Select-Object PsComputerName, InterfaceAlias, Status, MacAddress
}
$results | Export-csv -path C\users\bret.hooker\desktop\macaddress.csv -Append
Please note the base and filter are just examples and not the actual code due to work place confidentiality. Code currently will pull from AD all computer name, then will run the ForEach command to get the NetAdapter Information. I am unable to get it to output to the CSV file however. Any advice would be great.
My recommendations are 1) don't continuously append objects to an array, 2) avoid the -Append parameter of Export-Csv, and 3) take advantage of the pipeline. Example:
$computerNames = Get-ADComputer -Filter * -SearchBase "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM" | Select-Object -ExpandProperty Name
$computerNames | ForEach-Object {
Get-NetAdapter -CimSession $_ | Select-Object PSComputerName,InterfaceAlias,Status,MACAddress
} | Export-Csv "C\users\bret.hooker\desktop\macaddress.csv" -NoTypeInformation
So having some good old fashion Powershell frustrations today. What I need to do is this:
Get a list of computers from a file
Query those computers for "CSName" and "InstallDate" from Win32_OperatingSystem
Convert InstallDate into a useable date format.
Export all that to a .Csv
I've tried so many different iterations of my script. I run into 2 major issues. One is that I can't export and append to .Csv even with Export-Csv -Append. It just takes the first value and does nothing with the rest. The 2nd is that I can't get the datetime converter to work when piping |.
Here's a few samples of what I've tried - none of which work.
This sample simply errors a lot. Doesn't seem to carry $_ over from the WMI query in the pipe. It looks like it is trying to use data from the first pipe, but I'm not sure.
Get-Content -Path .\Computernames.txt | Foreach-Object {
gwmi Win32_OperatingSystem -ComputerName $_) |
Select-Object $_.CSName, $_.ConvertToDateTime($OS.InstallDate).ToShortDateString()
} | Export-Csv -Path Filename -Force -Append -NoTypeInformation
}
This one simply exports the first value and gives up on the rest when exporting .Csv
$Computers = Get-Content -Path .\Computernames.txt
foreach ($Computer in $Computers) {
echo $Computer
$OS = gwmi Win32_OperatingSystem -ComputerName $Computer
$OS | Select-Object
$OS.CSName,$OS.ConvertToDateTime($OS.InstallDate).ToShortDateString() |
Export-Csv -Path $Log.FullName -Append
}
This one does get the data, but when I try to select anything, I get null values, but I can echo just fine.
$OS = gwmi Win32_OperatingSystem -ComputerName $Computers
$OS | Foreach-Object {
Select-Object $_.CSName,$_.ConvertToDateTime($OS.InstallDate).ToShortDateString() |
Export-Csv -Path $Log.FullName -Force -Append -NoTypeInformation
}
This feels like it should be ridiculously simple. I can do this in C# with almost no effort, but I just can't get PS to do what I want. Any help would be much appreciated!
Here you go,
$Array = #() ## Create Array to hold the Data
$Computers = Get-Content -Path .\Computernames.txt
foreach ($Computer in $Computers)
{
$Result = "" | Select CSName,InstallDate ## Create Object to hold the data
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer
$Result.CSName = $OS.CSName ## Add CSName to line1
$Result.InstallDate = $OS.ConvertToDateTime($OS.InstallDate).ToShortDateString() ## Add InstallDate to line2
$Array += $Result ## Add the data to the array
}
$Array = Export-Csv c:\file.csv -NoTypeInformation
I've been working on a simple script to read the win32_product off a remote PC, which is working fine. However, I would like the query to ignore some common applications on my domain. I've been building a list of apps and their IdentifyingNumber and putting the IdentifyingNumber into a txt file. I load the text file into a variable with the script and I'm trying to figure out how to get the query to filter each item in the variable...so I have this::
$PC = Read-Host "What is target workstation..."
$logfile = "d:\$PC.txt"
$ignore = [IO.File]::ReadAllText("D:\INCOMING\AppListing\ignore.txt")
get-wmiobject -class win32_product -computer $PC | where {$_.IdentifyingNumber -notlike $ignore} | Select Name, IdentifyingNumber | sort-object Name | export-csv $logfile -encoding "unicode"
However, this is not filtering at all, not even the first or last item from the txt file. I used write-host $ignore to verify it is loading the items...but I am at a lost as to how to make this work. Perhaps a foreach loop? I can't find anything about putting a foreach loop into a where filter though...
Thanks for the assistance...
If the file is like this:
aRandomId
anotherRandonId
...
with one id on each line and nothing else, then try this using -notlike with wildcards on the ends. Ex:
$PC = Read-Host "What is target workstation..."
$logfile = "d:\$PC.txt"
$ignore = [IO.File]::ReadAllText("D:\INCOMING\AppListing\ignore.txt")
get-wmiobject -class win32_product -computer $PC | where { $ignore -notlike "*$($_.identifyingnumber)*" } |
Select Name, IdentifyingNumber | sort-object Name | export-csv $logfile -encoding "unicode"
You could also read your file as an array using ReadAllLines like you would have had to do if you wanted to use a foreach-loop or -notcontains. Ex:
$PC = Read-Host "What is target workstation..."
$logfile = "d:\$PC.txt"
$ignore = [IO.File]::ReadAllLines("D:\INCOMING\AppListing\ignore.txt")
get-wmiobject -class win32_product -computer $PC | where { $ignore -notcontains $_.identifyingnumber } |
Select Name, IdentifyingNumber | sort-object Name | export-csv $logfile -encoding "unicode"
$prods = Compare-Object -ReferenceObject (Get-Content $file) -DifferenceObject ((Get-WmiObject -Class Win32_Product -Computer $computer).IdentifyingNumber) -PassThru
Compare-Object is a great Cmdlet.