Ouput Server Name Along With InstallState of Service - powershell

I'm pretty new to Powershell. I have the following simple little script:
$serversall = (Get-Content ".\Servers.txt")
foreach($vm in $serversall){
Get-WindowsFeature Web-Server -ComputerName $vm |
Select-Object InstallState |
Export-Csv -path E:\Scripts\Output\IISWebServersStatus.csv -Append
}
This just gives me one column to let me know if the status is "Installed" or "Available". I want another column that would also show the server name next to the status of the service. Any idea how I would accomplish that?

I would recommend the following approach:
$serversall = (Get-Content ".\Servers.txt")
$output = #()
foreach($vm in $serversall) {
$installed = (Get-WindowsFeature Web-Server -ComputerName $vm).InstallState
$output += New-Object PSCustomObject -Property #{'ComputerName'=$vm; 'Status'=$installed }
}
$output | Export-Csv -path E:\Scripts\Output\IISWebServersStatus.csv -Append
Create an array, $output that you can use to store all the information. On each loop iteration, build an object that holds the the server name and the install state and append it to the output array.
After the loop is done, write the output array to the csv file.
Doing one file write at the end will save on i/o operations, so could save time. In this example, Get-WindowsFeature is a relatively slow operation so it probably makes little difference, but good to understand the theory nonetheless.

Related

Get-ChildItem on Multiple Computers, Performance Issues

I'm wanting to improve on my script to be able to accomplish the following:
Scan servers based on get-adcomputer on specific OUs.
Scan each server based on whatever drive letter it has.
Scan each server for log4j.
Export all results to a CSV that identifies the folder path, name of file, and the server that the file was found on.
I have been using the following code to start with:
$Servers = Get-ADComputer -Filter * -SearchBase "OU=..." | Select -ExpandProperty Name
foreach ($server in $Servers){
Invoke-Command -ComputerName $Server -ScriptBlock {
$Drives = (Get-PSDrive -PSProvider FileSystem).Root
foreach ($drive in $Drives){
Get-ChildItem -Path $drive -Force -Filter *log4j* -ErrorAction SilentlyContinue | '
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName
$Folder = $_.PSIsContainer
$Age = $_.CreationTime
$Path | Select-Object `
#{n="Name";e={$Item}}, `
#{n="Created";e={$Age}},`
#{n="FilePath";e={$Path}},`
#{n="Extension";e={if($Folder){"Folder"}else{$Type}}}`
} | Export-Csv C:\Results.csv -NoType
}
}
I am having the following issues and would like to address them to learn.
How would I be able to get the CSV to appear the way I want, but have it collect the information and store it on my machine instead of having it on each local server?
I have noticed extreme performance issues on the remote hosts when running this. WinRM takes 100% of the processor while it is running. I have tried -Include first, then -Filter, but to no avail. How can this be improved so that at worst, it's solely my workstation that's eating the performance hit?
What exactly do the ` marks do?
I agree with #SantiagoSquarzon - that's going to be a performance hit.
Consider using writing a function to run Get-ChildItem recursively with the -MaxDepth parameter, including a Start-Sleep command to pause occasionally. Also, you may want to note this link
You'd also want to Export-CSV to a shared network drive to collect all the machines' results.
The backticks indicate a continuation of the line, like \ in bash.
Finally, consider using a Scheduled Task or start a powershell sub-process with a lowered process priority, maybe that will help?

How do you store a list of users (C:\Users) in PowerShell to use later on in the same script?

I am having issues trying to figure out how to make this script work. I need to get a list (either stored in powershell somehow or saved to a text file) of users from the C:\Users folder and then run a script deleting certain cache data in each of those users folders. Because I am running this based off of a list that I pulled from AD (into another .txt file), I am having trouble understanding how to run it based off of a foreach, foreach concept where there are two foreach statements in one script.
$Computers = Get-Content "C:\Temp\Cache Cleanup Project\June 10 Lists\ComputerUp.txt" | foreach ($Computer in $Computers) {
dir \\$Computer\C$\Users | select Name | Out-File "C:\temp cache cleanup project\Computer Users\$Computer.txt"
I feel like I am missing something in the script above. What I need is a folder filled with .txt files with the name of each computer name that is in the ComputerUp.txt file. In each file will be a list of users for that specific machine. My other scripts will need to reference each of these files in order to go through and clear out the cache'd files.
I hope this makes sense. I have to clean up caches on near 700 machines and each user profile has to be scrubbed and this was the best way I could figure out how to do it. If anyone has any better suggestions, I am all ears.
What I need is a folder filled with .txt files with the name of each computer name that is in the ComputerUp.txt file
Using the file system for this is unnecessary - you can store this information in a variable in-memory instead:
# Read the list of computers from disk
$Computers = Get-Content .\path\to\ComputerUp.txt
# Create a dictionary to hold ComputerName->ListOfUsers data
$UsersPerComputer = [ordered]#{}
foreach($computer in $Computers){
# enumerate the remote folder names
$listOfNames = Get-ChildItem \\$Computer\C$\Users |Select-Object -ExpandProperty Name
# assign the list to the dictionary, use the computer name as the key
$UsersPerComputer[$computer] = $listOfNames
}
Now you can retrieve the list for a single computer by name:
$targetMachine = 'Computer123'
$UsersPerComputer[$targetMachine] # this will resolve to the list of user folder names we got from Computer123
Seems like you're confusing foreach with ForeEach-Object.
With foreach your script would look like this:
$Computers = Get-Content "C:\Temp\Cache Cleanup Project\June 10 Lists\ComputerUp.txt"
foreach ($Computer in $Computers)
{
(Get-ChildItem "\\$Computer\C$\Users").Name |
Out-File "C:\temp cache cleanup project\Computer Users\$Computer.txt"
}
With ForEach-Object it would look like this:
Get-Content "C:\Temp\Cache Cleanup Project\June 10 Lists\ComputerUp.txt" | ForEach-Object {
(Get-ChildItem "\\$_\C$\Users").Name |
Out-File "C:\temp cache cleanup project\Computer Users\$_.txt"
}
As a side note, doing this should get the job done exponentially faster:
$Computers = Get-Content "C:\Temp\Cache Cleanup Project\June 10 Lists\ComputerUp.txt"
$result = Invoke-Command -ComputerName $Computers -ScriptBlock {
Get-ChildItem C:\Users
}
$result | Group-Object PSComputerName | ForEach-Object {
$_.Group.Name | Out-File "C:\temp cache cleanup project\Computer Users\$($_.Name).txt"
}

Adding each item from for loop to csv file

I am trying to use power shell to determine whether a server has a particular patch installed based on the KB and if not append the name to a csv. my input file has system names so I want to export that system name if it does not find the patch installed.
here is what i have so far. The export to csv part does not seem to work.
forEach-Object{
try{
$status = wmic /node:#sys.csv qfe list full /format:table | findstr /i $kb_number
if(!$status){
$output_file = New-Item C:\temp\$kb_number.csv -ItemType File
export-csv $output_file -append -Force
}
else{
write-output $status
}
}
catch{
$error_message = $_.Exception.Message
#write-output "the error message is" $error_message
write-output "Could not find any system with this patch installed."
}
}
Why your code might be failing
We don't see where you're setting the values of #sys.csv or $kb_number in the code you shared. Either of those could be throwing you off.
But the real issue is Export-Csv. For one, you're making a new CSV with every iteration of the loop. And for two, you have to pass in some item for the cmdlet to export as a CSV. Right now, you're only providing these values.
$output_file = New-Item C:\temp\$kb_number.csv -ItemType File
Export-csv -Path $output_file -append -Force
Export-Csv requires an input object. You're not giving it one now.
What do you want to export? If you just want a list of computers without a patch, do this instead.
if(-not(Test-path C:\temp\$kb_number.csv)){
#if file doesn't exist, make it
$output_file = New-Item C:\temp\$kb_number.txt -ItemType File
}
#adds computer name if it doesn't have the patch
Add-Content -Path $output_file -Value $computer
General Suggestions
Instead of using ForEach-Object, you might find it's easier to debug if you use a ForEach loop like this.
$computers = Get-Content C:\pathTo\Your\ComputerList.txt
ForEach($computer in $computers){
}
One additional source of trouble is that your code is using older dos commands in WMIC and then tries to use PowerShell to store the records. You don't need to do this and can make it easier on yourself if you swap out the calls to wmic for Get-WmiObject or Get-CimInstance, the PowerShell native versions of the commands.
To do that, change this line:
wmic /node:#sys.csv qfe list full /format:table | findstr /i $kb_number
translates into
$kb_number = "KB4576484"
Get-CimInstance Win32_QuickFixEngineering -Filter "HotfixID = '$kb_number'" -ComputerName $computer
Source Description HotFixID InstalledBy InstalledOn
------ ----------- -------- ----------- -----------
Update KB4576484 NT AUTHORITY\SYSTEM 9/14/2020 12:00:00 AM
You can store the output of that in a variable and then call Export-Csv on it and that should work.
When in doubt, remove the filter part and just get it working to export all patches to a csv. Then add complexity by adding back the filtering statements.

Printer Migration - Powershell script

I have found some great examples on foreach loops in Powershell here but I just can't wrap my head around foreach loops for what I am doing.
I found great scripts that deal with migrating printer when migrating from one Windows print server to another however my challenge is that I am migrating from an Novell iPrint server to a Windows server.
The struggle is that the printer name or share name (or any printer property) for iPrint printer is not the hostname so I have to come up with some translation table with iPrint name and Printer hostname.
Initially, I wanted to just have column 2 of my translation table have it execute my powershell command to install a network printer which would make things easier.
I am in the process of trying to create a logon script to query printers that are installed on computer and have it do a 'foreach' loop against a CSV with iPrint names and hostnames.
csv 1
installediprintprintername1
installediprintprintername2
installediprintprintername3
printtranslationtable.csv
column 1 column 2
iprintprintername1 hostnameprinter1
iprintprintername2 hostnameprinter2
iprintprintername3 hostnameprinter3
iprintprintername4 hostnameprinter4
This is what I got so far but not able to get it to work. Any help would be appreciated!
$printers = #(Get-wmiobject win32_printer)
$path = "\\networkdrive\printtranslationtable.csv"
$printertranslation = Import-Csv -path $path
foreach ($iprintprinter in $printtranslationtable) {
foreach ($name in $csv1) {
if ($name -eq $printtranslationtable.column1) {
Write-Host $newPrinter = $printtranslationtable.column2
}
}
}
Update
So I was able to tweak the script #TheMadTechnician suggested and able to get this PS script to work in my environment. What I am trying to do is to check if new printers are installed and if they are then just exit script. This is what I have but can't get it to exit or break. I was also trying to write the new printers into text file but not necessary, I would like for it to stop executing script.
if (($printers.name -like "\winprint*") -eq $true) {
$printers.name -like "\winprint\" | out-file -FilePath "C:\windowsprinters.txt" -Append
{break} {exit}
}
When you read the file with Import-Csv, PowerShell creates an array of custom objects with property names from the header line. On the other hand Get-Content produces simple array of string values. I came up with this one liner, which goes thru the translation table and checks if the printer list contains one. This is not optimal if you have billions of printers, but keeps things clear:
printers.txt:
iprinter2
iprinter3
printertable.csv:
"Column1";"Column2"
"iprinter1";"hostname1"
"iprinter2";"hostname2"
"iprinter3";"hostname3"
"iprinter4";"hostname4"
PowerShell:
$printers = Get-Content .\printers.txt
$prtable = Import-Csv -Delimiter ";" .\printertable.csv
$prtable | ?{ $printers -contains $_.Column1 } | %{Write-Host "Install $($_.Column2)"}
Ok, so you query what printers are installed, and you have a translation table loaded from a CSV, now you just need to look at that translation table and cross reference which entries have a listing in the local computer's printer listings.
$printers = #(Get-wmiobject win32_printer)
$path = "\\networkdrive\printtranslationtable.csv"
$printertranslation = Import-Csv -path $path
$printertranslation | Where{$_.Column1 -in $printers.ShareName} | ForEach{ Add-Printer $_.Column2 }
I don't know what property of the win32_printer object aligns best for you, but I would suggest ShareName or DeviceId. Those should be something like:
ShareName: XeroxColor02
DeviceId: \\printserver\XeroxColor02

Process list and write output to files named after list members

I'm trying to run a command which accepts a list of names. It should go through each member of the list and then output it to a text file in a specific location, and the name of that text file will be the member name from the list. Then the script continues to the next member run the command on it, and then write the output to a text file whose name will be the 2nd member in the list.
I'm sure a loop is involved, and perhaps a temporary variable which I have no idea how to declare :(
Invoke-Command -ComputerName (Get-Content "C:\ServerList.txt") -ScriptBlock {
Servermanagercmd.exe -query roles.xml
} -credential baloon\yellow | Out-File C:\OutputF.txt
Pull out the Get-Content of the serverlist file so the server name values are available down the pipeline:
Get-Content c:\serverlist.txt | Where {$_} |
Foreach { Invoke-Command -ComputerName $_ -Scriptblock {...} -Credential baloon\yellow |
Out-File "$_.txt" }
Note that the Where {$_} is to weed out any empty lines in the file.
Shall I assume this issue is that the data that is being output has no context since you wont know what system it is from?
$outputfile = C:\OutputF.txt
Set-Content -Path $outputfile -Value ""
ForEach($server in (Get-Content "C:\ServerList.txt")){
Add-Content -Path $outputfile -Value "Server: $server"
Invoke-Command -ComputerName $server -ScriptBlock {Servermanagercmd.exe -query roles.xml} -credential baloon\yellow |
Add-Content -Path $outputfile
}
Wipe the file to start new (remove this if you dont want to erase the file.). For each server in your list add the server name on its own line in the file and then run the Invoke-Command also sending its output to file.
This will might be inefficient depending on the number of server you are checking. At that point I would consider using jobs.
Update
After reading Keith Hills answer I realize that I misread your question. My logic would output all to one file with the server name separating the contents. You actually wanted separate files.