Powershell, invoke-command against entire network/ IP block.

I am using the following command, which is working great, but it is only good. per machine.

invoke-command -computer 4w68cm2 -filepath "\\server\files\temp\ToastNotificationWin10.ps1"


Is there a way to do the same command, but instead of specifying a single machine, it would "somehow" specify an entire IP block.

For example, the command is ran against machines in the 10.10.2.x network.
LVL 2
Glenn MSystems Engineer / ManagerAsked:
Who is Participating?
 
ITguy565Connect With a Mentor Commented:
I haven't tested this, But this should work for you as well and check for live hosts.
$cred = get-credential
$liveComputers = 1..254 | % {test-connection -computer "10.10.2.$_" -BufferSize 16 -Count 1 -ea 0}
$LiveComputers = $livecomputers |Select-Object Address -ExpandProperty Address

Foreach ($Computer in $LiveComputers){
write-host "This ran on $computer"
    invoke-command -computer $Computer -credential $cred -filepath "\\server\files\temp\ToastNotificationWin10.ps1"
}

Open in new window

0
 
David SankovskyConnect With a Mentor Senior SysAdminCommented:
try this:

$intStartingAddress = 1

$intEndingAddress = 254

$Subnet = "10.10.2."

for ($i = $intStartingAddress; $i -le $intEndingAddress; $i++) {
    $target = $Subnet + $i
    invoke-command -computer $target -filepath "\\server\files\temp\ToastNotificationWin10.ps1"
}

Open in new window


You can change the starting and ending address as needed and of course the subnet.
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
@ITguy565

I've executed your script with some minor network changes d, and it prompted for creds. I enter my domain admin creds and this was the results:

$cred = get-credential
$liveComputers = 1..252 | % {test-connection -computer "10.10.8.$_" -BufferSize 16 -Count 1 -ea 0}
$LiveComputers = $livecomputers |Select-Object Address -ExpandProperty Address

Foreach ($Computer in $LiveComputers){
write-host "This ran on $computer"
    invoke-command -computer $Computer -credential $cred -filepath "C:\temp\resultone.ps1"
}

Open in new window


PS C:\Temp> C:\Temp\PS10.ps1
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:


...and it just sits there.
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
Glenn MSystems Engineer / ManagerAuthor Commented:
@David Sankovsky

I ran your code, with some Ip changes.


$intStartingAddress = 1

$intEndingAddress = 252

$Subnet = "10.10.8."

for ($i = $intStartingAddress; $i -le $intEndingAddress; $i++) {
    $target = $Subnet + $i
    invoke-command -computer $target -filepath "C:\temp\resultone.ps1"
}

Open in new window



...and I expected it to trigger my machine, as I am in that subnet, and I got this error:



[10.10.8.96] Connecting to remote server 10.10.8.96 failed with the following error message : The WinRM client cannot process the request. Default authentication may be used with an IP
address under the following 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 TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm help config. For more
information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (10.10.8.96:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : CannotUseIPAddress,PSSessionStateBroken
0
 
ITguy565Commented:
Do you have a lot of dead addresses on your network..

The script goes out and checks every address in that subnet to make sure it is active. Saves the list to the Variable $LiveComputers and then afterwards attempts to run the invoke command on each of those $Computers.
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
@ITguy565

It actually finished, just took a while. On my machine, I got this error:

This ran on 10.10.8.95
[10.10.8.95] Connecting to remote server 10.10.8.95 failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (10.10.8.95:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
@ITguy565
I ran the script from the machine it gave the error from.
0
 
ITguy565Commented:
Was trying to avoid the Access is Denied errors by having you enter your credentials $cred.
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
@ITguy565

Agreed, but it didn't work on my own machine. That part is strange.

In case you are worrying about the "resultone.ps1" script itself, it is down below and works create if executed as Admin Windows PowerShell.

$app = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]

$Template = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText01

#Gets the Template XML so we can manipulate the values
[xml]$ToastTemplate = ([Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($Template).GetXml())

$alertFile = ".\alert.txt"
[xml]$ToastTemplate = @"
<toast launch="action=viewAlarm&amp;alarmId=3" scenario="alarm">

  <visual>
    <binding template="ToastGeneric">
      <text>* Notification *</text>
      
<text>$(Get-Content $alertFile | Out-String)</text>
<image placement="appLogoOverride" hint-crop="circle" src="https://picsum.photos/64?image=000"/>
            <image placement="hero" src="http://finalbca.16mb.com/noti2.png"/>
    </binding>
  </visual>

  <actions>

     <action
      activationType="system"
      arguments="snooze"
      content=""/>

    <action
      activationType="background"
      arguments="dismiss"
      content="Dismiss"/>

  </actions>

</toast>
"@



$ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$ToastXml.LoadXml($ToastTemplate.OuterXml)

$notify = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($app)

$notify.Show($ToastXml)

Open in new window

0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
@ITguy565

confession, I didn't test this using PS as an Admin, like I thought I did. Ran it as Admin, and it worked as expected.
Question, is there a way to modify the timeout of the test-connection feature?
0
 
ITguy565Commented:
that's as much as i can modify it.. i am sending only a single ping to the boxes.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.