I'm working on script which collects information about computer (os version, software installed etc.) and I would like to get output in CSV file to be able to import in SQL later.
Currently, I have something like this:
$os = Get-WmiObject Win32_OperatingSystem
$soft = Get-WmiObject Win32_Product
$customerid = $env:UserName
$osversion = $os.Caption
$osplatform = $os.OSArchitecture
$ossp = $os.servicepackmajorversion
$timestamp = Get-Date -Format "dd.MM.yyyy HH:mm"
$timestamp,$customerid, $osversion, $osplatform, $ossp, $soft.Name, $soft.Version | Out-File C:\test.csv
I would prefer output file something like this:
http://s9.postimg.org/5zmwoda4f/image.png
but at the moment I'm gettting all information this way:
http://s12.postimg.org/9gsrvdfz1/image.png
How can I achieve output like in the first image?
Thank you for any help!
What you are seeing is how the pipe is dealing with arrays. To get the output you desire we need to make some other changes since you didn't make any effort to add titles and what not. This output deviates slightly from what you desire since it is easier to have the software title on its own line.
$os = Get-WmiObject Win32_OperatingSystem
$soft = Get-WmiObject Win32_Product
$customerid = $env:UserName
$osversion = $os.Caption
$osplatform = $os.OSArchitecture
$ossp = $os.servicepackmajorversion
$timestamp = Get-Date -Format "dd.MM.yyyy HH:mm"
$softwareDetails = ($soft | ForEach-Object{",$($_.Name),,,,,$($_.Version)"})
"Time:,$timestamp",
"UserName:,$customerid",
"OS Version,$osversion",
"Architecture,$osplatform",
"Service Pack,$ossp",
"Software Versions",
$softwareDetails | Out-File C:\test.csv
Since we are using arrays for output each entry will be on it's own line. I would have suggested using Export-CSV but your desired output does not match a traditional csv file.
Related
I'm trying to Run Advertised Programs using PowerShell
$tpObject = Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
| Select-Object -Property PKG_Manufacturer, PKG_Name, PKG_MIFVersion
The output will be:
PKG_Manufacturer PKG_Name PKG_MIFVersion
---------------- -------- --------------
Microsoft Word v1234
Google Chrome v987
Microsoft Excel v987
etc
How do I concatenate it into a string? I tried this:
[string[]]$result = $tpObject.PKG_Manufacturer + $tpObject.PKG_Name + " - " + $tpObject.PKG_MIFVersion
$result
But it display all the PKG_Manufacturer, then PKG_Name, then PKG_MIFVersion
I would like it to display this, Microsoft Word - v1234 as a string?
Any suggestions or comments would be greatly appreciated.
tks
Give this a try:
$result=#()
Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution | %{
$result += "$($_.PKG_Manufacturer) $($_.PKG_Name) - $($_.PKG_MIFVersion)"}
$result
I guess what you want is something like this
$list = New-Object 'System.Collections.Generic.List[string]'
Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
| ForEach-Object $list.Add("$($_.PKG_Manufacturer) $($_.PKG_Name) - $($_.PKG_MIFVersion)")
To concatenate and aggregate the values in a string array you have several options.
First, you can concatenate a string:
You can concat strings in PowerShell by using the + operator (as applied by you)
You can use the -f formatting approach (like in C#) "My PC {0} has {1} MB of memory." -f "L001", "4096"
Or you can use a double quoted string with variables $x = "Max"; Write-Output "I'm $x" (Hind: Sometimes you need sub-expressions expansion, as shown in my example above. See the Windows PowerShell Language Specification Version 3.0, p34).
Second, you can aggregate your results:
Using a dynamically-typed array $testArray = #() with $testArray += $_
Using a typed array [string[]]$testArray = [string[]] also with += as shown below.
Using a generic List $list = New-Object 'System.Collections.Generic.List[string]' with the Add-Method $list.Add($_)
And there is more...
In general, I would try to stay with the PS Objects as long as possible.
Aggregating stuff in a string array to process it later is too much (C#) programmer thinking.
In your Code:
1. You cannot add a new array element with the = operator, use += instead:
$testArray = #()
$tempArray = "123", "321", "453"
$tempArray | ForEach {$testArray += $item }
$testArray
$tpObject = Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
$tpobject | ForEach-Object{
"{0} {1} - {2}" -f $_.PKG_Manufacturer, $_.PKG_Name, $_.PKG_MIFVersion
}
See details of the -f format operator
Sample output:
Microsoft Word - v1234
Google Chrome - v987
Microsoft Excel - v987
$OSInfo = get-wmiobject -class win32_operatingsystem -computername c83323
($OSInfo `
| Format-List `
#{Name="OS Boot Time";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}} `
| Out-String).Trim() #Output OS Name, Boot Time, and Install Date
output --> OS Boot Time : 12/6/2016 4:09:20 PM
$osinfo = get-wmiobject -class win32_operatingsystem -computername c83323
$osinfo.ConvertToDateTime($osinfo.LastBootUpTime)
output --> Tuesday, December 06, 2016 4:09:20 PM
Why is it when I run the first set, I get time in one way, but when I run it the 2nd way, I get it in a completely different format?
This is because you are using Format-List and/or Out-String in the first case. When using these, PowerShell formats the DateTime object's output, just as if you wrote this:
"$(Get-Date)"
The output of your second instance will be in DateTime type. This format depends on the datetime format you have Choosen on your system.
I modified your code to get the type:
$osinfo = get-wmiobject -class win32_operatingsystem; ($osinfo.ConvertToDateTime($osinfo.LastBootUpTime)).GetType()
But in your first instance, you use something called a calculated property (check this link) , which basically let you "Format" and display attributes in the way you prefer. In your case, thru the expression you have provided your date and time had converted into an array and hence it lost its format.
Get type:
($OSInfo | Format-List #{Name="OS Boot Time";Expression={$_.ConvertToDateTime($_.LastBootUpTime)}}).GetType()
The above type would be an array.
EDIT:
Below snippet should do the trick!
#("Computer1","Computer2") | foreach {
$OSInfo = get-wmiobject -class win32_operatingsystem -computername $_;
"Boot time for Computer: $_ = " + $OSInfo.ConvertToDateTime($OSInfo.LastBootUpTime);
} | Out-File "C:\thefolderYouPrefer\boottime.txt"
I am querying remote servers for their operating system. I know that I can return the Version, but I want to replace these values with the friendly name. The code I have so far is:
$Computer = (gc c:\servers.txt)
$BuildVersion = Get-WmiObject -Class Win32_OperatingSystem -Property Version, CSName -ComputerName $Computer -ErrorAction SilentlyContinue
$Build=$BuildVersion.version
If ({$BuildVersion.Version -match "5.2.3790"})
{$Build="2003"}
Elseif ({$BuildVersion.Version -match "6.1.7601"})
{$Build="2008"}
Elseif ({$BuildVersion.Version -like "6.3.9600"})
{$Build="2012"}
But this doesn't seem to work and only returns "2003" regardless. Please help, I'm fairly new to PS and coding.
thanks
The problem is your if statements. Putting the Boolean expression inside squiggly brackets makes it a script block, and that's going to get cast as a string before being cast as a Boolean. Strings cast to Booleans always evaluate to true unless they're empty.
PS C:\> {$BuildVersion.Version -match "5.2.3790"}
$BuildVersion.Version -match "5.2.3790"
PS C:\> ({$BuildVersion.Version -match "5.2.3790"}) -as [bool]
True
PS C:\> $BuildVersion.Version -match "5.2.3790"
False
PS C:\> ($BuildVersion.Version -match "5.2.3790") -as [bool]
False
So what you're running is essentially:
if ([bool]'$BuildVersion.Version -match "5.2.3790"') [...]
And that's always going to be true.
Try:
$Computer = (gc c:\servers.txt)
$BuildVersion = Get-WmiObject -Class Win32_OperatingSystem -Property Version, CSName -ComputerName $Computer -ErrorAction SilentlyContinue
$Build=$BuildVersion.version
If ($BuildVersion.Version -match "5.2.3790")
{
$Build = "2003"
}
Elseif ($BuildVersion.Version -match "6.1.7601")
{
$Build = "2008"
}
Elseif ($BuildVersion.Version -like "6.3.9600")
{
$Build = "2012"
}
Bottom line is that squiggly brackets are not parentheses and you can't use them like they are.
However, there's also a major logic error here. You're potentially fetching an array for $BuildVersion because you're reading from a file, but then you treat it like a single value. You never loop through $BuildVersion. However, I do not have enough information about what you're actually trying to do with your script (like what you do with $Build) to be able to fix that.
I originally said this, but I've since changed my mind
The reason this is only returning 2003 is that you're only running your If code on a single entry in the list.
Wrong
As TessellatingHeckler says, the reason your if wasn't working is that you had too many curly braces, so PowerShell wasn't actually evaluating your logic.
However, you still need to step through each of the computers to do what you're trying to do. We'll do that by adding in a ForEach loop. I also went ahead and replaced your If {} logic with a Switch statement, which I think is easier to understand for a scenario like this with multiple clauses. If's just get way too verbose.
Finally, I'm assuming you want to output the results too, so I added a custom object here, which is just a way of choosing which properties we want to display.
$Computer = (gc c:\servers.txt)
ForEach ($system in $computer){
$BuildVersion = Get-WmiObject -Class Win32_OperatingSystem -Property Version, CSName -ComputerName $system -ErrorAction SilentlyContinue
$Build=$BuildVersion.version
switch ($build){
"5.2.3790" {$Build="2003"}
"6.1.7601" {$Build="2008"}
"6.3.9600" {$Build="2012"}
}
#output results
[pscustomobject]#{Server=$system;OSVersion=$build;CSName=$buildVersion.CSname}
}#EndOfForEach
Output
>Server OSVersion CSName
------ --------- ------
dc2012 2012 DC2012
sccm1511 2012 SCCM1511
You can use this:
Get-WmiObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty Caption
Additionally you can see everything this WMI object holds like this:
Get-WmiObject -Class Win32_OperatingSystem | fl *
Edit: if you want to remove some text from the string, you can use -replace:
(Get-WmiObject -Class Win32_OperatingSystem |
Select-Object -ExpandProperty Caption) -replace "Microsoft Windows Server ",""
Trying to get the difference in days between to dates: Today's date. and a date/time from a wmiobject (this was taken from a post from the PendingReboot script from Hey, Scripting! blog):
$Lastreboottime = Get-WmiObject win32_operatingsystem -ComputerName $Computer |
select csname, #{LABEL='LastBootUpTime';EXPRESSION=$_.ConverttoDateTime($_.lastbootuptime)}}
$Today = Get-Date -Format d
$DiffDays = $Today - $Lastreboottime
The result of $Today is
09/06/2016
and $Lastreboottime is
05/05/2016 11:13:21
So I want to get rid of the time but not sure how to do this.
Secondly, I get this error if I were to run the script, though I guess this may go away if I am able to extract the date only in $Lastreboot
Cannot convert the "#{csname=JDWTAWEB1; LastBootUpTime=05/05/2016 11:13:21}" value of type "Selected.System.Management.ManagementObject" to type "System.DateTime".
Any ideas?
Remove -Format d and compare the Date-properties of the DateTime-objects to get the days-diff only.
Your $Lastreboottime-variable references an object with both computername csname and the LastBootUpTime, so you need to access the LastBootUpTime
Try:
$Lastreboottime = Get-WmiObject win32_operatingsystem |
select csname, #{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
$Today = Get-Date
$DiffDays = $Today.Date - $Lastreboottime.LastBootUpTime.Date
$DiffDays.TotalDays
13
I think that the WMIObject conversion might need to get to a Datetime object by way of a properly formatted string. I did this (minus the -Computername $Computer part) and it seemed to work.
[string]$BootTimeString=(Get-WmiObject win32_operatingsystem -ComputerName $Computer).lastbootuptime -replace '\..*',''
$BootTimeDT=[datetime]::ParseExact($BootTimeString,'yyyyMMddHHmmss',$null)
$DiffDays = (NEW-TIMESPAN –Start $BootTimeDT –End (Get-Date)).Days
Remove -Format d from Get-Date. You need DateTime object, not a string.
$Lastreboottime is an object with 2 properties: csname and lastbootuptime. You have to use lastbootuptime property.
Example:
$Today = Get-Date
$DiffDays = $Today - $Lastreboottime.lastbootuptime
I am trying to get a list of servers and the last time they rebooted to show in a table. However, if it doesn't respond to a ping, I just need it to show in the list. I can't seem to figure out how to get it to add to the table after else.
Import-CSV $Downtime | % {
if(Test-Connection $_.server -Quiet -count 1){
Get-WmiObject Win32_OperatingSystem -ComputerName $_.server |
select #{LABEL="Name"; EXPRESSION = {$_.PSComputerName}}, #{LABEL="Last Bootup"; EXPRESSION = {$_.convertToDateTime($_.LastBootupTime)}}
}
else{#{LABEL="Name"; EXPRESSION = {$_.server}}
}
} | Out-GridView
I can always save the else results in a text file but this would be more convenient.
You need to make the same object, with the same properties!, in both cases so that PowerShell will understand the association between the two. The follwing example builds a custom hashtable using the if/else and outputs the object for each loop pass.
Import-CSV $Downtime | ForEach-Object {
$props = #{}
$server = $_.server
if(Test-Connection $server -Quiet -count 1){
$wmi= Get-WmiObject Win32_OperatingSystem -ComputerName $server
$props.Name = $wmi.PSComputerName
$props."Last Bootup" = $wmi.convertToDateTime($wmi.LastBootupTime)
}else{
$props.Name = $server
$props."Last Bootup" = "Could not contact"
}
New-Object -TypeName psobject -Property $props
} | Out-GridView
I used $server as the $_ changes context a couple of time so we wanted to be able to refer to the current row in the CSV we are processing.
I don't know what your PowerShell version is so I will assume 2.0 and create objects that support that.
In both cases an object is created with a Name and Last Bootup property which is populated based on the success of the ping.
As an aside I had a similar question a while ago about created similar object based output.