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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David SankovskySenior 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
ITguy565Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.