So i have the bottom scriptblock:
Import-csv C:\file_location.csv | foreach-object {
Get-WmiObject -computername $_.computername -class Win32_ComputerSystem | select username} | `
Select-Object computername, username | Export-CSV C:\file_location.csv -notypeinformation
the exported csv shows the Computer Name header but no actual computer and the username header is just fine. What and where am I missing something from?
Thank You!
select (which is an alias for Select-Object) returns an object with only the properties you specify. So when you did your first select username you got an object with just the username; all other properties were discarded, so when the second Select-Object call runs, there is no computername for it to return.
The first select seems completely unnecessary there; just take it out and I think everything will work as expected.
EDIT: I see now that computername is not a property of the returned WMI object; it came from the CSV. Your ForEach-Object is only returning the WMI object, so the CSV row object is being discarded.
What you need to do is add the computername from the CSV to the WMI object, which you can do with Select-Object (with a computed column) or Add-Member:
Import-csv C:\file_location.csv |
ForEach-Object {
Get-WmiObject -computername $_.computername -class Win32_ComputerSystem |
Select-Object #{Name='ComputerName';Expression={$_.computername}},username
} |
Export-CSV C:\file_location.csv -notypeinformation
Or:
Import-csv C:\file_location.csv |
ForEach-Object {
Get-WmiObject -computername $_.computername -class Win32_ComputerSystem |
Add-Member -NotePropertyName computername -NotePropertyValue $_.computername -Force -PassThru
} |
Select-Object computername, username
Export-CSV C:\file_location.csv -notypeinformation
Related
Noob here, I need to extract some data from a cim interrogation of a list of servers, however the csv output is just the same reiteration of the local server, with the sum total equaling the number of lines in the input file. I think I'm doing something wrong with the array but Ive been beating my head. Help?
$ErrorActionPreference = 'SilentlyContinue'
#Define Some Variables
$importpath = "c:\directory1"
$workingpath = "c:\directory2"
#Do Some Filtering
Import-CSV -Path "$importpath\somefile.csv" | where {$_.Powerstate -ne "PoweredOff"} | where Guest -notlike *somestring* | Export-Csv "$workingPath\PRODUCTION.csv" -NoTypeInformation
Import-csv -Path "$workingPath\PRODUCTION.csv" | Select-Object -Property Name | Export-Csv -Path "$workingpath\SERVERS.csv" -NoTypeInformation
#Create final input foreach routine
Import-CSV -Path "$workingpath\SERVERS.csv" | Out-file $workingpath\SERVERS.txt
$servers = Get-Content -path "$workingpath\SERVERS.txt"
$results = foreach ($server in $servers) {
Get-CimInstance -ClassName win32_OperatingSystem -ErrorAction SilentlyContinue | Select-Object *
}
$results | Export-Csv -Path "$workingPath\PRODUCTIONRESULTS.csv" -NoTypeInformation -Append
Try including the computername when you run the Get-CimInstance command
$results = foreach ($server in $servers) {
Get-CimInstance -ComputerName $server -ClassName win32_OperatingSystem -ErrorAction SilentlyContinue | Select-Object *
}
I have an Inventory Powershell script that I am trying to output into a csv. My goal is to output each execution of the script in a separate row going down their columns.
I've tried Export-Csv, but since i'm using variables, the csv displays (i'g guessing) metadata.
$ComputerName = Get-WmiObject Win32_OperatingSystem | select -ExpandProperty CSName
$OS_Name = Get-WmiObject Win32_OperatingSystem | Select-Object -ExpandProperty Caption
$OS_Architecture = Get-WmiObject Win32_OperatingSystem | select -ExpandProperty OSArchitecture
$System_Manufacturer = Get-WmiObject win32_computersystem | select -ExpandProperty Manufacturer
$Model = Get-WmiObject win32_computersystem | select -ExpandProperty Model
$CPU_Manufacturer = Get-WmiObject Win32_Processor | select -ExpandProperty Name
$Disk_Size_GB = Get-WmiObject win32_diskDrive | Measure-Object -Property Size -Sum | % {[math]::round(($_.sum /1GB),2)}
$Physical_Memory_GB = Get-WMIObject -class Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | % {[Math]::Round(($_.sum / 1GB),2)}
$Version=(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
$InstallDate= systeminfo | find /I “Install Date”
$Assettag=(Get-WmiObject -Class Win32_SystemEnclosure | Select-Object SMBiosAssetTag).SMBiosAssetTag
$SerialNumber = (Get-WmiObject -Class Win32_BIOS | Select-Object SerialNumber).SerialNumber
($Assettag, $ComputerName, $System_Manufacturer, $Model, $OS_Name, $Version, $SerialNumber | Format-Table | Out-File C:\Users\1\Desktop\Newinvent.csv )
I expect each row to be filled with each execution
Thanks
If you want to use Export-Csv, you need to have an object with properties that contain the names and values you want to export. One way to do this is to create a [PSCustomObject] with all of your properties defined in a hash table. You can pipe that custom object to the Export-Csv command.
[PSCustomObject]#{"ComputerName" = $ComputerName
"OS_Name" = $OS_Name
"OS_Architecture" = $OS_Architecture
"System_Manufacturer" = $System_Manufacturer
"Model" = $Model
"CPU_Manufacturer" = $CPU_Manufacturer
"Disk_Size_GB" = $Disk_Size_GB
"Physical_Memory_GB" = $Physical_Memory_GB
"Version" = $Version
"InstallDate" = $InstallDate
"Assettag" = $Assettag
"SerialNumber" = $SerialNumber
} | Export-Csv -Path file.csv -NoTypeInformation -Append
And try to look at the result from Get-ComputerInfo - most of your needs are satisfied there.
I'd avoid the time consuming Systeminfo
> (Measure-Command {$systeminfo=(systeminfo) 2>$NULL}).Totalseconds
3,0428012
As well as Get-ComputerInfo
$OldProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
(Measure-Command {$ComputerInfo = Get-ComputerInfo}).Totalseconds
3,1837208
$ProgressPreference = $OldProgressPreference
And use an optimized version of your script which gets/converts InstallDate from registry:
> (Measure-Command{Q:\Test\2019\06\03\SO_56429703.ps1}).TotalSeconds
1,635074
Get-Content file.csv
## Q:\Test\2019\06\03\SO_56429703.ps1
function RoundGB($Size){
[math]::round(($Size/1GB),2)
}
$Win32OS = Get-WmiObject Win32_OperatingSystem
$Win32CS = Get-WmiObject Win32_ComputerSystem
$CurrVer = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
$InstDate= [timezone]::CurrentTimeZone.ToLocalTime([datetime]'1/1/1970').AddSeconds(
(Get-ItemProperty $CurrVer).InstallDate)
$inventory = [PSCustomObject]#{
ComputerName = $Win32OS.CSName
OSName = $Win32OS.Caption
OSArchitecture = $Win32OS.OSArchitecture
SystemManufacturer= $Win32CS.Manufacturer
Model = $Win32CS.Model
CPUManufacturer = (Get-WmiObject Win32_Processor).Name
DiskSizeGB = RoundGB (Get-WmiObject Win32_DiskDrive | Measure-Object Size -Sum).Sum
PhysicalMemoryGB = RoundGB (Get-WMIObject Win32_PhysicalMemory | Measure-Object capacity -Sum).Sum
Version = (Get-ItemProperty -Path $CurrVer -Name ReleaseId).ReleaseId
InstallDate = $InstDate
Assettag = (Get-WmiObject Win32_SystemEnclosure).SMBiosAssetTag
SerialNumber = (Get-WmiObject Win32_BIOS).SerialNumber
}
$Inventory | Export-Csv -Path file.csv -NoTypeInformation -Append
I want to get all of those users starting with a specific keywords in AD User Description field using PowerShell.
Keyword: Could not execute powershell
Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase 'OU=contoso, DC=contoso, DC=local' - Properties Description | select -expand name,distinguishedname,description | Export-Csv -path C:\description.csv -NoTypeInformation
Last Update :
Get-ADUser -Properties Description -Filter 'Description -like "*Could not execute powershell*"' -SearchBase 'OU=contoso, DC=contoso, DC=local' |
select name,distinguishedname,description |
Export-Csv -path C:\description2.csv -NoTypeInformation
This is an easy task using the -filter option that you can use with get-aduser.
For more info how to filter: https://technet.microsoft.com/en-us/library/ee617241.aspx?f=255&MSPPError=-2147217396
Under the filter bit
Get-ADUser -Properties Description -Filter {Description -like $Description} -SearchBase 'OU=contoso, DC=contoso, DC=local' | select Name, DistinguishedName, Description | Export-Csv -path C:\description2.csv -NoTypeInformation
You can simply filter for Description:
Get-ADUser -Properties Description -Filter 'Description -like "*Could not execute powershell*"' -SearchBase 'OU=contoso, DC=contoso, DC=local'
I am trying to let the script below:
Get all the installed software on a number of computers and Sort by computer name, and not list all the software all together.
This is my code -
param(
[string[]]$computername='testpc'
)
Get-WmiObject -computername $computername -Class Win32_Product | Select-Object -Property Name, description, installdate, vendor | Sort-Object Name > C:\desktop-apps.txt
You can add the PSComputerName property to your Select-Object and Sort-Object calls:
Get-WmiObject -computername $computername -Class Win32_Product |
Select-Object -Property PSComputerName, Name, description, installdate, vendor |
Sort-Object PSComputerName, Name
I wanted to extract a list of users logged on to remote pc, the ps names would be fed in using a .csv file.
I was able to get a command
Get-WmiObject Win32_LoggedOnUser -ComputerName $Computer | Select Antecedent -Unique
to query the user names, could any one help me more on how to write this code?
Assuming the csv file contains a ComputerName header:
Import-Csv computers.csv | Foreach-Object{
Get-WmiObject Win32_LoggedOnUser -ComputerName $_.ComputerName | Select-Object __SERVER,Antecedent -Unique | Foreach-Object {
$domain,$user = [regex]::matches($_.Antecedent,'="([^"]+)"') | Foreach-Object {$_.Groups[1].Value}
$_ | Select-Object __SERVER,#{Name='Domain';Expression={$domain}},#{Name='User';Expression={$user}}
}
}