I am new to powershell scriptting and what it can do. I have an IP address of remote server and I want to retrieve the date and time of that on my local machine which is connected to it using cisco any connect . Can I do it using powershell.
-
1Next time put a sample of your script, and people can help you modify it. That's sort of what stackoverflow is all about – Robert Cotterman Sep 09 '18 at 22:18
-
can anyone from the community tell me why this question is getting so much negative votes and what should I improve while asking my next question – Deepankur Singh Sep 11 '18 at 16:02
-
My comment above said why. Stack overflow is to help code get better. Not write code for you. So people get angry and downvote if you haven't "tried" already. – Robert Cotterman Oct 08 '18 at 05:12
2 Answers
FYI, remoting through WinRM via IP, requires a -credential
paramater value to be set. (Do this with $admin = Get-Credential
and use -credential $admin
as a parameter in the invoke-command)You can not remote via ip with your current credentials. Also when using invoke command, I would HIGHLY suggest using -sessionoption (new-pssessionoption -nomachineprofile)
otherwise you're creating profiles on every machine, which not only takes more time, but also leaves massive trails of your work.
E.g. (with Nas answer used)
$admin = Get-Credential
$remoteIP = 'x.x.x.x'
Invoke-Command -ComputerName $remoteIP -credential $admin -sessionoption (new-pssessionoption -nomachineprofile) -ScriptBlock {
$env:COMPUTERNAME
Get-Date
}
if you want to use a ton of machines, you could do this...
$admin = Get-Credential
$remoteIP = "x.x.x.x","x.x.x.x"
foreach ($pc in $remoteIP) {
Invoke-Command -ComputerName $pc -credential $admin -sessionoption (new-pssessionoption -nomachineprofile) -ScriptBlock {
[pscustomobject][ordered]@{'Hostname'=$env:COMPUTERNAME
'Date'=Get-Date}
}
}
to speed it up, i would remove "env:computername" as it should return pscomputername anyhow
this should be tons faster
$remoteIP = "x.x.x.x","x.x.x.x"
foreach ($pc in $remoteIP) {
$object = Get-WmiObject -Class win32_operatingsystem -ComputerName $pc -Property localdatetime,__server
[pscustomobject][ordered]@{'computername'=$object.__server
'Date'=$object.converttodatetime($object.localdatetime)}
}
__Server is TWO underscores than the word server. NOT _Server but __Server
[pscustomobject] is short for new-object -typename psobject -property ...
and [ordered] just ensures your hashtable order is preserved.

- 2,213
- 2
- 10
- 19
-
1You don't need to loop through an array. `-ComputerName` takes an array of strings already. – Maximilian Burszley Sep 09 '18 at 23:12
-
You are ABSOLUTELY correct. BUT, pscustomobject needs to handle one item at a time. – Robert Cotterman Sep 10 '18 at 03:31
-
I also tried to get it to spit out date time and not need a forloop, but the select @{n;e} whole thing wouldn't work for me – Robert Cotterman Sep 10 '18 at 03:50
-
Finally, `get-wmiobject` is such a root command, that it has zero impact on time(meaning to get it started). running it against localhost 1000 times(in an array), took 26.434 seconds with OUT a for loop, and 26.601 seconds with a for loop. finally, using pscustomobject on the latter made it 27.052 seconds. so 600 milliseconds per 1000 attempts. in other words it's negligible... the only benefit i could maybe see is if you set throttle limit maybe...tested it, and throttle limit had zero impact for me. Maybe on a network it might, but i can't test at this time – Robert Cotterman Sep 10 '18 at 04:31
-
also note, function processes commands just like foreach loops by default, so if he turns it into a function, it will be 1 at a time from his input – Robert Cotterman Sep 10 '18 at 05:27
Try this, should work if you have the necessary permissions and psremoting is enabled on the target server (Edit: convert ip to hostname)
$remoteIP = 'x.x.x.x'
$remoteServer = [System.Net.Dns]::GetHostByAddress($remoteIP)
Invoke-Command -ComputerName $remoteServer.HostName -ScriptBlock {
$env:COMPUTERNAME
Get-Date
}
Try it and we can help you from there, depending on the outcome.

- 1,243
- 6
- 7
-
Thanks for answering the question ! I don't have permissions to send or run script request on server from ip communications. But I will try it as soon as I will get the permissions – Deepankur Singh Sep 09 '18 at 18:25
-
Use `help about_Remote*` to get a list of documentation. `about_Remote_Requirements` is a good place to start. – lit Sep 09 '18 at 19:16
-
Depending on why this doesn't work for you, this might also help [Powershell remoting with ip-address as target](https://stackoverflow.com/a/6667923/7554519) – Jacob Sep 09 '18 at 20:45