I have run the below command to get the output that when was my machine last patched but also need how to get the KB details too and how can I export in csv from SCCM.
$lastpatch=Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object HotfixID, InstalledOn | Select-Object -first 1
"{0:MM/dd/yyyy}" -f $lastpatch.InstalledOn | Write-Output
When I run the below command to also get the HotfixID but I am not getting the result so can someone help me with it.
$lastpatch= Get-HotFix | Sort-Object InstalledOn -Descending | select-object -first 1 | Select-Object InstalledOn, HotfixID
"{0:MM/dd/yyyy}" -f $lastpatch.InstalledOn,$lastpatch.HotfixID | Write-Output
To include the HotfixID, you can make use of a calculated property to have the Select-Object return an object with both wanted properties into the variable $lastPatch.
Once you have that object, it is easy to export to csv:
$lastpatch = Get-HotFix |
Sort-Object InstalledOn -Descending |
Select-Object -First 1 |
Select-Object #{Name = 'InstalledOn'; Expression = {"{0:MM/dd/yyyy}" -f $_.InstalledOn}},
HotfixID
# Output on screen
$lastPatch
# output to csv file
$lastPatch | Export-Csv -Path 'Path\To\lastpatch.csv' -UseCulture -NoTypeInformation
Related
How do I properly use $_ in out-file? Here's my code:
get-content computers.txt |
Where {$_ -AND (Test-Connection $_ -Quiet)} |
foreach { Get-Hotfix -computername $_ } |
Select CSName,Description,HotFixID,InstalledBy,InstalledOn |
convertto-csv | out-file "C:\$_.csv"
I'm trying to execute a get-hotfix for all the computers listed in the text file then I want them to be exported to CSV with the computer name as the filename.
You need one pipeline to process the computers.txt files, and a nested one inside the foreach to process the list of hotfixes for each computer:
get-content .\computers.txt |
Where {$_ -AND (Test-Connection $_ -Quiet)} |
foreach {
Get-Hotfix -computername $_ |
Select CSName,Description,HotFixID,InstalledBy,InstalledOn |
convertto-csv | out-file "C:\$_.csv"
}
Edit: Changed computers.txt to .\computers.txt, as this is required for local paths in powershell
i can see with this:
get-content .\computers.txt | Where {$_ -AND (Test-Connection $_ -Quiet)} | foreach{ Get-Hotfix -id KB4012212 -computername $_ | Select CSName,Description,HotFixID,InstalledBy,InstalledOn | convertto-csv | out-file "C:\$_.csv" }
i can see only in which PC is the fix (KB4012212) installed.
it's possible to see the following
CSNAME Fix(Inst/NotInst)
PC1 FIxInstalled
PC2 FixNotinstalled
PC3 FixnotInstalled
..
..
etc
I monkeyed with this for a while and nothing I found on-line worked until I used this combo.
I used the method is this thread but it was SO slow and I wanted to learn more about using jobs so this is what ended up working for me on Windows 7 PS Ver 4.
All other options were either too slow or did not return data from the remote system.
$VMs = get-content C:\WinVms.txt #Generate your hostnames list however you deem best.
foreach ($vm in $vms)
{
Write-Host "Attempting to get hotfixes on:" $vm
invoke-command -computername $vm -ScriptBlock {start-job -scriptblock {(get-hotfix | sort installedon)[-1]} | wait-job | receive-job} -AsJob
}
start-sleep 60 # give it a minute to complete
get-job | ? { $_.state -eq "Completed"} | receive-job -keep | export-csv c:\temp\win-patch.csv
you can check your failures too like this:
get-job | ? { $_.state -eq "Failed"}
I need a script to pull data from another. Here's what I have:
$myvar = (cat "C:\ProgramData\CrashPlan\log\app.log" | Select-String -pattern "<path include=")
$v = $myvar -replace '.*='
$vv = $v -replace '/>'
$first = $vv | select-object -first 1
$second = $vv | select-object -skip 1 | select-object -first 1
$third = $vv | select-object -skip 2 | select-object -first 1
$fourth = $vv | select-object -skip 3 | select-object -first 1
$fifth = $vv | select-object -skip 4 | select-object -first 1
cd "C:\pcheck\CheckPermissions"
.\CheckPermissions.ps1 $first
When I run the above, I get "directory not found" even after I verify the output of $first is "E:\" Any idea how to fix this?
Thanks!
Select-string returns an object with many properties. I think you want the line property.
$myvar = (cat "C:\ProgramData\CrashPlan\log\app.log" |
Select-String -pattern "<path include=").line
I want the greatest value (mailboxSize) at the top of the file. I have a cvs as inport.
When I do the following sort cmd:
Import-Csv import.csv| Sort-Object MailboxSize,DisplayName -Descending | Export-Csv SORT.csv
I get the following result:
"DisplayName","MailboxSize"
"persone6","9941"
"persone3","8484"
"persone1","7008"
"persone4","4322"
"persone5","3106"
"persone7","27536"
"persone10","24253"
"persone8","1961"
"persone9","17076"
"persone11","17012"
"persone2","15351"
"persone12","11795"
"persone14","1156"
"persone13","1008"
But I want this as a result!
"persone7","27536"
"persone10","24253"
"persone9","17076"
"persone11","17012"
"persone2","15351"
"persone12","11795"
"persone6","9941"
"persone3","8484"
"persone1","7008"
"persone4","4322"
"persone5","3106"
"persone14","1156"
"persone13","1008"
When importing a CSV-file, all properties are made string-type. You have to cast the MailboxSize to an int before you can sort it properly. Try:
Import-Csv import.csv |
Sort-Object {[int]$_.MailboxSize}, DisplayName -Descending |
Export-Csv SORT.csv
You should also use the -NoTypeInformation switch in Export-CSV to avoid the #TYPE ..... line (first line in an exported CSV-file).
Sample:
$data = #"
"DisplayName","MailboxSize"
"persone6","9941"
"persone3","8484"
"persone1","7008"
"persone4","4322"
"persone5","3106"
"persone7","27536"
"persone10","24253"
"persone8","1961"
"persone9","17076"
"persone11","17012"
"persone2","15351"
"persone12","11795"
"persone14","1156"
"persone13","1008"
"# | ConvertFrom-Csv
$data |
Sort-Object {[int]$_.MailboxSize}, DisplayName -Descending |
Export-Csv SORT.csv -NoTypeInformation
SORT.csv
"DisplayName","MailboxSize"
"persone7","27536"
"persone10","24253"
"persone9","17076"
"persone11","17012"
"persone2","15351"
"persone12","11795"
"persone6","9941"
"persone3","8484"
"persone1","7008"
"persone4","4322"
"persone5","3106"
"persone8","1961"
"persone14","1156"
"persone13","1008"
I'm guessing the usernames are fake, but be aware that the same issue goes for DisplayName if your usernames actually was personeXX where XX is an int. Like:
persone7 27536
persone20 27536
persone13 27536
To sort them probably, you'd have to create a scriptblock for Sort-Object or create your own function to split the value and sort them correctly.
I am running the below code to get all mailboxes sizes:
Get-MailboxStatistics -Server 583606-exchange| Sort-Object TotalItemSize -Descending | ft DisplayName,#{label="TotalItemSize(GB)";expression={"$([math]::round($_.TotalItemSize.Value.ToBytes() /1Gb, 2)) GB"}} ,ItemCount -auto
This is the output:
DisplayName___TotalItemSize(GB)___ItemCount
User1_________14.71 GB__________83000
User2__________3.98 GB__________7600
Now if I need to export the output to excel using the below code:
Get-MailboxStatistics -Server servername| Sort-Object TotalItemSize -Descending | ft DisplayName,#{label="TotalItemSize(GB)";expression={"$([math]::round($_.TotalItemSize.Value.ToBytes() /1Gb, 2)) GB"}} ,ItemCount -auto | export-csv C:\Scripts\UserMailboxSizes.csv
it's exporting but no data in there.
Can you please let me know what I am missing?
Thanks in advance :)
Yousry
Try this:
Get-MailboxStatistics -Server servername| Sort -Property TotalItemSize -Descending | Select-Object DisplayName,#{label="TotalItemSize(GB)";expression={"$([math]::round($_.TotalItemSize.Value.ToBytes() /1Gb, 2)) GB"}} ,ItemCount -auto | export-csv C:\Scripts\UserMailboxSizes.csv
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}}
}
}