Asked by:
Powershell script to extract basic services from remote VM's

Question
-
I am actually new to power shell and wanted to learn more. I have small task assigned to me for which i need your help. I have VMs that I need to check the Windows services,IP address, Mac address,check connectivity to some server, routing tables, . I have created a script in power shell but i am getting the below error can you help me Enter-PSSession : Connecting to remote server failed with the following error message : The WinRM cl ient cannot complete the operation within the time specified. Check if the machine name is valid and is reachable over the network and firewall exception for Windows Remote Management service is enabl ed. For more information, see the about_Remote_Troubleshooting Help topic.
- Moved by Bill_Stewart Wednesday, November 29, 2017 6:40 PM This is not "scripts on demand"
Tuesday, October 17, 2017 10:54 AM
All replies
-
Below is my code:
$Servers = Get-Content "c:\myfolder\ServerList.txt"
Invoke-Command -ScriptBlock {Set-ExecutionPolicy unrestricted -force}
ForEach ($Server in $Servers)
{
enable-psremoting -force
$username = "my username"
$password = ConvertTo-SecureString -String "My passsword" -AsPlainText -force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password
Enter-PSSession -ComputerName $server -Credential $cred
$hostn = hostname
Invoke-Command -ComputerName $Server {($g | where-object {$_.status -eq "running"})| ft > C:\script\detailtest2_$hostn.txt}
}
- Edited by Pradeep9191 Tuesday, October 17, 2017 5:05 PM
Tuesday, October 17, 2017 10:55 AM -
This is closer but what is $g?
$sb = { $g | where-object { $_.status -eq 'running' } } Get-Content c:\sungard_files\ServerList.txt | ForEach-Object{ Invoke-Command -ComputerName $_ -ScriptBlock $sb } | Format-Table > C:\script\detailtest2_$hostn.txt
We do not set-executionpolicy in the script. It is set once by Group Policy or by an Admin. If you area a Domain Admin then you won't need credentials.
\_(ツ)_/
- Marked as answer by Pradeep9191 Tuesday, October 17, 2017 4:45 PM
- Unmarked as answer by Pradeep9191 Tuesday, October 17, 2017 4:46 PM
Tuesday, October 17, 2017 1:09 PM -
Hi Thanks for the reply
No i don't have domain admin right
also I came across few machine where set execution policy as -Restricted instead of changing it manually i though to add that in the script.
and the $g is variable which i defined as
Invoke-Command -ComputerName $Server {($g = Get-Service | where-object {$_.status -eq "running"})| ft > C:\script\detailtest2_$hostn.txt}
Still the below error:-
Enter-PSSession : Connecting to remote server failed with the following error message : The WinRM cl
ient cannot complete the operation within the time specified. Check if the machine name is valid and
is reachable over the network and firewall exception for Windows Remote Management service is enabl
ed. For more information, see the about_Remote_Troubleshooting Help topic.
At C:\scripts\TestACSprecheck1.ps1:11 char:20
+ Enter-PSSession <<<< -ComputerName $server -Credential $cred
+ CategoryInfo : InvalidArgument: (10.137.177.88:String) [Enter-PSSession], PSRemoting
TransportException
+ FullyQualifiedErrorId : CreateRemoteRunspaceFailed
[10.137.177.88] Connecting to remote server failed with the following error message : The WinRM clie
nt cannot process the request. Default authentication may be used with an IP address under the follo
wing conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit
credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the Trust
edHosts list might not be authenticated. For more information on how to set TrustedHosts run the fol
lowing command: winrm help config. For more information, see the about_Remote_Troubleshooting Help t
opic.
+ CategoryInfo : OpenError: (:) [], PSRemotingTransportException
+ FullyQualifiedErrorId : PSSessionStateBroken
I am to connect and RDP the server 10.137.177.88.. No connectivity issue
- Edited by Pradeep9191 Tuesday, October 17, 2017 5:10 PM
Tuesday, October 17, 2017 5:04 PM -
You need to learn PowerShell before trying to do such technical things:
$sb = { Get-Service | where-object { $_.status -eq 'running' } } Get-Content c:\sungard_files\ServerList.txt | ForEach-Object{ Invoke-Command -ComputerName $_ -ScriptBlock $sb } | Format-Table > C:\script\detailtest2_$hostn.txt
\_(ツ)_/
Wednesday, October 18, 2017 1:27 AM