can you please help me?
I am just trying to get a list of all Servers on the Domain and for each server ask simultaneously which mssql server version is running on that server.
Thank you so much!
This is what a already have tried:
Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} -Property \* | foreach object Sqlcmd -Q "SELECT ##VERSION" | foreach object | Format-Table Name,OperatingSystem -Wrap -Auto | Export-CSV AllWindows.csv -NoTypeInformation -Encoding UTF8
I get this error:
The name "object" entered cannot be resolved to a method name.
Try this instead:
$youradmincreds = Get-Credential
Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} -Property * | %{
Invoke-Command -ComputerName $_.SamAccountName -Credential $youradmincreds -ScriptBlock {
$version = Sqlcmd -Q "SELECT ##VERSION"
$version | Format-Table Name,OperatingSystem -Wrap -Auto | Export-CSV AllWindows.csv -NoTypeInformation -Encoding UTF8
}
}
edit: and if you've got ThreadJob module available (which you can also just get wtih find-module ThreadJob | install-module), you can speed the job up
$youradmincreds = Get-Credential
$job = Start-ThreadJob -ScriptBlock {
Invoke-Command -ComputerName $input -Credential $youradmincreds -ScriptBlock {
$version = Sqlcmd -Q "SELECT ##VERSION"
$version | Format-Table Name,OperatingSystem -Wrap -Auto | Export-CSV AllWindows.csv -NoTypeInformation -Encoding UTF8
}
} -InputObject (Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} | select -ExpandProperty SamAccountName)
$job | Wait-Job | Receive-Job
Related
I need to generate a csv containing running services to csv with the corresponding computer name
I know there is a simple way to do this and I have been tinkering with creating a new psobject, but I am not sure how to pipe the results to the new-object...
Here is what I am using:
$Input = "SomePath"
$Output = "SomeOtherPath"
$CompNames = Get-Content -Path "$Input"
ForEach ($CompName in $CompNames){
Get-Service -ComputerName $CompName | Where-Object {$_.Status -eq "Running"} | Export-csv -Path "$Output"
}
What I need in the CSV is:
ComputerName, ServiceName, DisplayName
basically, I need to add the computer name to the array.
If you want to be able to pipe the results, use a foreach-object.
$Output = "SomeOtherPath"
Get-Content -Path "SomePath" | ForEach-Object {
Get-Service -ComputerName $_ | Where-Object {$_.Status -eq "Running"} | Select-Object ComputerName, ServiceName, DisplayName
} | Export-csv -Path "$Output"
If you want to stick to a foreach statement, collect it all first then export it.
$Output = "SomeOtherPath"
$CompNames = Get-Content -Path "SomePath"
$results = ForEach ($CompName in $CompNames){
Get-Service -ComputerName $CompName | Where-Object {$_.Status -eq "Running"} | Select-Object ComputerName, ServiceName, DisplayName
}
$results | Export-csv -Path "$Output"
Try like this (Don't use $Input as variable name)
$InputX = "SomePath"
$Output = "SomeOtherPath"
$CompNames = Get-Content -Path "$Input"
ForEach ($CompName in $CompNames){
Get-Service -ComputerName $CompName | Where-Object {$_.Status -eq "Running"} | Select-Object ComputerName, ServiceName, DisplayName | Export-csv -Path "$Output"
}
$ErrorActionPreference = 'SilentlyContinue'
$ComputerName =Get-ADComputer -Filter {(Name -like "*")} -SearchBase "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM" | Select-Object -ExpandProperty Name
$results = #{}
ForEach ($computer in $ComputerName) {
$Results += Get-NetAdapter -CimSession $ComputerName | Select-Object PsComputerName, InterfaceAlias, Status, MacAddress
}
$results | Export-csv -path C\users\bret.hooker\desktop\macaddress.csv -Append
Please note the base and filter are just examples and not the actual code due to work place confidentiality. Code currently will pull from AD all computer name, then will run the ForEach command to get the NetAdapter Information. I am unable to get it to output to the CSV file however. Any advice would be great.
My recommendations are 1) don't continuously append objects to an array, 2) avoid the -Append parameter of Export-Csv, and 3) take advantage of the pipeline. Example:
$computerNames = Get-ADComputer -Filter * -SearchBase "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM" | Select-Object -ExpandProperty Name
$computerNames | ForEach-Object {
Get-NetAdapter -CimSession $_ | Select-Object PSComputerName,InterfaceAlias,Status,MACAddress
} | Export-Csv "C\users\bret.hooker\desktop\macaddress.csv" -NoTypeInformation
I would like to return output for below in rows and cols style not one after another.
$path = "$([Environment]::GetFolderPath("Desktop"))\Installed Hotfixes Info.txt";
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . | fl * | Format-Table -AutoSize |fl > $path; notepad $path;
Remove the Format-List (fl):
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . |
Format-Table -AutoSize |
Out-File $path
Better yet, export the output as CSV:
Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . |
Export-Csv $path -NoType
I'm using the following script and it creates the out file correctly, but it's empty. I'm not sure what it's missing exactly. This is my first attempt with using workflows.
workflow DisableIISParallel
{
$ScriptPath = "C:\Scripts\Server_Lists"
$OLD = "Legacy-Servers"
$OldList = Get-Content "$ScriptPath\$OLD.txt"
$objHost=$Records.Length
function DisableIIS ($appName) {
$objHostStr = [System.Net.Dns]::GetHostEntry([string]$objHost).HostName
Invoke-Command -ComputerName $objHostStr { iisreset /stop }
Set-Service -Name W3SVC -StartupType Disabled -Status Stopped -ComputerName $objHostStr
Get-WmiObject -Class win32_service -ComputerName $objHostStr |
where { $_.name -eq "W3SVC" } |
Format-Table -Property #{Expression={$_.PSComputerName};Label="Server";width=18},
#{Expression={$_.Name};Label="Service";width=45},
#{Expression={$_.StartMode};Label="Mode";width=10},
#{Expression={$_.State};Label="State";width=10},
#{Expression={$_.Status};Label="Status";width=10} |
Format-List | out-string -s | ? {$_} | Out-File C:\Scripts\Output\$appName.log -Append
Write-Output "" | Out-File C:\Scripts\Output\$appName.log -Append
}
foreach -parallel($objHost in $OldList)
{
$appName = $OLD
DisableIIS $appName
}
}
DisableIISParallel
This bit is not right:
Get-WmiObject -Class win32_service -ComputerName $objHostStr |
where { $_.name -eq "W3SVC" } |
Format-Table -Property #{Expression={$_.PSComputerName};Label="Server";width=18},
#{Expression={$_.Name};Label="Service";width=45},
#{Expression={$_.StartMode};Label="Mode";width=10},
#{Expression={$_.State};Label="State";width=10},
#{Expression={$_.Status};Label="Status";width=10} |
Format-List | out-string -s | ? {$_} | Out-File C:\Scripts\Output\$appName.log -Append
You should not run the output of one format command (Format-Table) into another format command (Format-List).
Try this (assuming you want tabular format - if not, change the Format-Table to Format-List):
Get-WmiObject -Class win32_service -ComputerName $objHostStr |
where { $_.name -eq "W3SVC" } |
Format-Table #{Expression={$_.PSComputerName};Label="Server";width=18},
#{Expression={$_.Name};Label="Service";width=45},
#{Expression={$_.StartMode};Label="Mode";width=10},
#{Expression={$_.State};Label="State";width=10},
#{Expression={$_.Status};Label="Status";width=10} >> C:\Scripts\Output\$appName.log
And pass in all the variables required to the function e.g.:
function DisableIIS ($appName, $objHost) {
...
}
foreach -parallel($objHost in $OldList)
{
$appName = $OLD
DisableIIS $appName $objHost
}
I can only get the command to return the services on the first computer in the text file.
Is there a better way than for-each for this task?
Get-Service *vault* -ComputerName (Get-Content c:\users\sean\desktop\js.txt) | select name,status,machinename | sort machinename | format-table -autosize
Try it without the get-content. Try this:
Get-Service *vault* -ComputerName c:\users\sean\desktop\js.txt | select name,status,machinename | sort machinename | format-table -autosize
If that doesn't work, then try:
$Computers = Get-Content c:\users\sean\desktop\js.txt
Get-Service *vault* -computername $Computers | Select name,status,machinename |sort machinename |format-table -autosize
If you are eager for a one-liner then try this:
Get-Content c:\users\sean\desktop\js.txt | Get-Service *vault* | Select name,status,machinename |sort machinename |format-table -autosize
I would try the top one first. I would test, but I don't have access to anything I can do a proper test right now.
$Computers = get-content .\desktop\test.txt
$Service = "Vault"
foreach ($computer in $computers) {
$computer
$Servicestatus = get-service -name $Service -ComputerName $computer
}
$Servicestatus | select-object Name,Status,MachineName | format-table -Autosize
This works for me, it gives me each of the computers in the text file, and it looks for the service.
This is what I use. I get the list of computers from an OU in AD.
Import-Module ActiveDirectory
$ou = "OU=Servers,DC=Domain,DC=com"
$servers = Get-ADComputer -Filter * -SearchBase $ou | select-object -expandproperty name
Foreach ($server in $servers){
$Data = Get-Service -ServiceName *IIS*,*TomCat*,*httpd* -ComputerName $server | select machinename,name | sort machinename | format-table -AutoSize
Write($Data) | Out-File .\WebServices.txt -Append
}
$servers = Get-Content .\servers.txt
Foreach ($server in $servers) {
"$server"
Get-Service -ComputerName $Server -name -like "*vault*"
"-------------------"
}
Following a memory limitation limit with older versions of PowerShell, I was required to refresh my code:
Old code:
gwmi win32_service -computer $allcomputers | Select-Object __SERVER,Name,state,startmode,StartName
New code:
`$servers = Get-Content "computers.txt"
Foreach ($server in $servers) {
Get-WmiObject -Class WIN32_service -ComputerName $server |
Select-Object __SERVER,Name,state,startmode,StartName |
Export-Csv -path "Report.CSV" -NoTypeInformation -Append
}`
This is how you can get list of all services in your AD domain:
Get-ADComputer -Filter {OperatingSystem -Like “Windows 10*”} | ForEach-Object {Get-WmiObject -Class Win32_Service -Computer $_.Name}
More useful examples on this (get list of services for all computer listed in a text file, etc.):
https://www.action1.com/kb/list_of_services_on_remote_computer.html
Get-Service -ComputerName ... has a bug in PowerShell 2.0 that only returns the first computer. This is fixed in newer versions so if you upgrade to PowerShell 3.0 or newer, your original code will work fine.
As a workaround, use a foreach-loop to run Get-Service once for each computer:
Get-Content c:\users\sean\desktop\js.txt |
ForEach-Object { Get-Service -Name *vault* -ComputerName $_ } |
Select-Object -Property Name, Status, MachineName |
Sort-Object -Property MachineName |
Format-Table -AutoSize
Nick's solution totally doesn't work for me. I ended up writing a quick and dirty one that works:
$servers = Get-Content .\servers.txt
Foreach ($server in $servers) {
"$server"
Get-Service *vault*
"-------------------"
}