Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Powershell run a command\filename based on dnshostname

$
0
0

I am trying to run test-connection and ping computers in AD  I am able to do this but I would like to run wolcmd if I can't ping the computer.  I would create a script with the wolcmd  mac ip etc based on the result of my ping.  What is happening now is that I can ping and get a 'true ' 'false' result  .  When I get  a false result it calls a test script I created but it calls the script for all false pings.  I want it to run only if the computer name I can not ping matches my wolcmd script name.

Hope this makes sense.  I am new to Powershell and think I am missing something obvious.

here is what i have so far.  I have it outputting to the screen so  I can see what happens..some of this script I copied from the web.

Import-Module active*
$test=$null
Get-ADComputer  -filter * -Searchbase 'OU=Desktops,DC=aaa,DC=aaa,DC=aaa' |
ForEach-Object {
$test=test-connection -CN $_.dnshostname -count 1 -buffersize 16 -quiet
if($test -match 'true')  
{write-host -ForegroundColor green $_.dnshostname}
if($test -match 'false')
{write-host -ForegroundColor red $_.dnshostname}
if($test -match 'false')
{ get-childitem c:\scripts | where  $_.dnshostname -like $_.filename | & c:\scripts\*.bat}}

Below are others I have tried:

#| where "'*.bat'" -match $_.dnshostname }}
#if($test -match 'false' ) {Invoke-Command  {& c:\scripts\*.bat (where $_.dnshostname -like $_.filename)}}}


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>