0

I have seen a lot of these items out there but none seem to be for a user defined function.

I am new to powershell and have created a script to run the net view command and then test if I have access to the directory:

Function get-netview{
 param($hosts)
 (net view \\$hosts)  | % { if($_.IndexOf(' Disk ') -gt 0){ $_.Split('  ')[0] }}
 }

function fastping{
  [CmdletBinding()]
  param(
  [String]$computername = "127.0.0.1",
  [int]$delay = 100
  )

  $ping = new-object System.Net.NetworkInformation.Ping
  # see http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipstatus%28v=vs.110%29.aspx
  try {
    if ($ping.send($computername,$delay).status -ne "Success") {
      return $false;
    }
    else {
      return $true;
    }
  } catch {
    return $false;
  }
}

function get-dir{
param($hosts,$dir)
$errors=@()
$i = 0
Get-ChildItem \\$hosts\$dir -ea SilentlyContinue -ErrorVariable +errors | Out-Null
if ($errors.count -gt 0){
$outvar = "Access Denied",$hosts,$dir,"0" 
$outvar –join “,” | Out-File .\outfile.txt -append
}
else{
$x = (Get-ChildItem \\$hosts\$dir).count
if ($x -gt 0) {
$outvar = "Access Granted",$hosts,$dir,$x 
$outvar –join “,” | out-file .\outfile.txt -append
}
else{
write-host ("Empty Folder",$hosts,$dir,"0") -Separator ","
$outvar = "Empty Folder",$hosts,$dir,"0" 
$outvar –join “,” | Out-File .\zzfile.txt -append
}
}
}

function run-netviewall{
param($hosts)
$pings = fastping $hosts 250
if ($pings) {
foreach ($dir in get-netview $hosts) {get-dir $hosts $dir | out-file temtest.txt}
}else{
$hosts | out-file .\notonline.txt
}
}

These are the 4 function I have loaded and they work when i Run them on a single host but when i try to multithread them I get an error that ""The term 'run-netviewall' is not recognized as the name of a cmdlet, function, script file, or operable program.

Any help would be appreciated. Some of the scripts above were obtained online and edited slightly to meet my needs but they work. Any help you can provide would be appreciated.

FYI I have tried 4 different multithreaders and they all give me that error. I'm sure its stomething simple I didn't know about to

Thanks, Jon

  • You haven't described *how* you're "multithreading" them. I suspect what's happening is you're starting additional runspaces for those threads and as a result, your functions don't exist in them. – alroc Jan 15 '15 at 15:08
  • I am multithreading through scriptblock http://stackoverflow.com/questions/21520094/powershell-throttle-multi-thread-jobs-via-job-completion – user116180 Jan 15 '15 at 15:25
  • You are likely correct on some of the multithread scripts I have tried use runspaces... is there a way to make them work in those runspaces? – user116180 Jan 15 '15 at 15:28
  • Your function(s) must be defined in the `scriptblock` that you pass to `start-job` – alroc Jan 15 '15 at 15:38
  • That works thanks didn't think about that and the script runs perfectly to scan my network for open shares – user116180 Jan 23 '15 at 15:58

0 Answers0