Powershell - query servers with roles installed - powershell-2.0

I have a txt file with all the server names.
I want to query each server in the txt to see if RDS is installed. So far it works fine. But the export needs to have servername and the installed role. how can I pass the servername to the output file.
At the moment it is querying but only the installed. so with a 100 servers who really can tell which server has it installed and which doesnt>>>> PLEase help
Import-Module ServerManager
Get-Content W:\AllWindows.txt | ForEach-Object {Get-WindowsFeature -Name Remote-Desktop-Services} | Format-Table -Auto -wrap | Out-File -FilePath $csvfile

Try something like this:
Get-Content W:\AllWindows.txt | ForEach-Object { #(($_), (Get-WindowsFeature -Name Remote-Desktop-Services)) } | Format-Table -Auto -wrap | Out-File -FilePath $csvfile

I have worked it out:
Get-Content W:\windows2012.csv | Foreach-Object {{Get-WindowsFeature | where-object {$_.Installed -eq $True} | Export-Csv -Path "W:\output\$_.txt"}

Related

Error-Parameter set cannot be resolved using the specified named parameters

Hope you can help me with a problem trying to execute a script block
Get-AzAksVersion -Location CanadaCentral | Where-Object -Property OrchestratorVersion -gt 1.22.11, IsPreview -ne $true
trying to get the versions greater than the current version and excluding the preview version, getting error as Parameter set cannot be resolved. Thanks in advance.
I tried to reproduce the same in my environment and got the same error as below:
Get-AzAksVersion -Location CanadaCentral | Where-Object -Property OrchestratorVersion -gt 1.22.11, IsPreview -ne $true
To resolve the error, try the below command:
Get-AzAksVersion -Location CanadaCentral | where-Object {($_.OrchestratorVersion -gt '1.22.15') -and ($_.IsPreview -ne 'True')}

Delete long name path folders older than 7 days

I need to make a script that it will be delete folders and files(all things), but only older than 7 days. I have a code, but it has a problem when path name is longer than 256 chars. One of the user has a many folders, name lenght equal 303 chars.
Below code works on Windows 10, but has problem on Windows 2008R2(.net 4.7.2) with \\?\....
Get-ChildItem -Path '\\?\c:\test\users' | ?{ $_.CreationTime -le $(Get-Date).AddDays(-7) } | Remove-Item -recurse -force
And that has problem with long path name:
Get-ChildItem D:\foler\folder\file | ?{ $_.CreationTime -le $(Get-Date).AddDays(-7) } | Remove-Item -Force -Recurse
Get-ChildItem D:\foler\folder\file | ?{ $_.CreationTime -le $(Get-Date).AddDays(-7) } | Remove-Item -Force -Recurse
Also I try with robocopy. So will be ok any script on powershell, .bat or .vbs.

How do I turn this simple command into something that loops from a list of remote systems?

I received a request to run this on each of my systems which pulls a list of installed applications and outputs it into a text file. I then have to combine all of these things into something more readable which will take a while. I am learning Powershell and want to make this be executed from one system, pull from a list of servers in a text file and run this query from one place against all of the systems:
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize > "$Env:userprofile\desktop\Installed Programs for $env:computername.txt"
I've started working on it but am thinking I am missing something to get this to work. I am currently piping this to a string to then output to a csv (I am open to suggestions). This is what I have so far.
# Computer running this script
$WhoAmI = $env:ComputerName
$ServerList = get-content -path "C:\scripts\ServerList.txt"
$Path = "C:\scripts\results"
foreach ($server in $ServerList)
{
$InstalledApps = Invoke-Command -ComputerName $server {Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* }
$Results += $InstalledApps |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Out-String
}
Write-Host $InstallApps
# $InstallApps | export-csv "$Path\InstalledFiles.csv"
I currently am testing the functionality of the loop by just trying to get it to write to the screen. I only get a blank response.
You weren't getting output because you used the (undefined) variable $InstallApps instead of the variable $results.
With that said, I wouldn't recommend doing string concatenation in a loop. Something like this would be a more elegant approach:
Get-Content -Path 'C:\scripts\ServerList.txt' | ForEach-Object {
$server = $_
Invoke-Command -ComputerName $server -ScriptBlock {
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
} | Select-Object #{n='Server';e={$server}}, DisplayName, DisplayVersion,
Publisher, InstallDate
} | Export-Csv 'C:\scripts\results\InstalledPrograms.csv' -NoType
I kind of figured it out. Rested eyes on a fresh day. Some mistakes in what I am writing out, etc. I am open to improvements if anyone has anything to contribute.
EDIT: I got it working mostly with the following but the output is messy. Open to suggestions.
# Computer running this script
$ServerList = get-content -path "C:\scripts\ServerList.txt"
$Path = "C:\scripts\results"
foreach ($server in $ServerList)
{
$Results += "Results for $server"
$InstalledApps = Invoke-Command -ComputerName $server -ScriptBlock {Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* }
$Results += $InstalledApps |
Select DisplayName, DisplayVersion, Publisher, InstallDate |
Out-String
}
# Write-Host $Results
$Results | out-file -filepath "$Path\InstalledPrograms.txt" -width 200

What is wrong with my script? Deletes all files -- Powershell

I want to delete files recursively, while keeping the seven most recent ones. The following script deletes all files, even though I specify the skip parameter.
$files = gci -path f:\inbox\test -recurse | where {!$_.PsIsContainer}
foreach ($file in $files) {sort creationtime -desc | select -skip 7 | remove-item -path $file.FullName -force}
This script doesn't do a damn thing:
gci -path f:\inbox\test -recurse | where {!$_.PsIsContainer} | foreach-object {sort creationtime -desc | select -skip 7 | remove-item -force }
try this
$files = gci -path f:\inbox\test -recurse | where {!$_.PsIsContainer} | sort creationtime -desc | select -skip 7
foreach ($file in $files) {remove-item -path $file.FullName -force}
or more simply
gci -path f:\inbox\test -recurse | where {!$_.PsIsContainer} | sort creationtime -desc |
select -skip 7 | remove-item -force

Export content from ForEach-Object

I am attempting to create a PowerShell command that finds and exports the owner of each distribution list provided in a CVS.
[PS] C:\WINDOWS\system32>Import-Csv $path_import | ForEach-Object {Get-QADGroup $_.Identity | select ManagedBy,Name | Export-Csv $path_export}
Currently the script will output to the screen the information I am trying to capture but only export the last line.
Any assistance would be appreciated.
try this:
Import-Csv $path_import | ForEach-Object {Get-QADGroup $_.Identity |
select ManagedBy,Name } | Export-Csv $path_export -notypeinformation

Resources