PowerShell pass a switch to a function with Invoke-Command -


i'm having difficulties passing switch -cleanfolders function using invoke-command. found this, don't know how implement it's not targeted function.

calling function works fine:

delete-oldfiles $target $olderthandays $server -cleanfolders invoke-command -computername "$server" -authentication credssp -credential $credentials -scriptblock ${function:delete-oldfiles} -argumentlist ($target, $olderthandays, $server) 

but doesn't work @ all:

invoke-command -computername "$server" -authentication credssp -credential $credentials -scriptblock ${function:delete-oldfiles} -argumentlist ($target, $olderthandays, $server, -cleanfolders) 

full script:

#__________________________________________________________________________________________________________________________________ $importfile = "s:\input\scheduled task\auto_clean.csv" $password = cat "s:\input\pwd.txt" | convertto-securestring -force $username = "domain\me"  #__________________________________________________________________________________________________________________________________ # scriptblock running function in job $jobfunc = { # function removes files older x days in subfolders function delete-oldfiles { <#  .synopsis        script delete files , folders older x days  .description     remove files older x days in subfolders , write success , failure actions logfile in "\\deustheidit02\log\scheduled task\auto_clean\".      default, empty foldes left behind , not deleted.  .parameter target      path recusively scanned old files  .parameter olderthandays     filter age of file, entered in days. use 0 files removed  .parameter cleanfolders     if switch specified folders older 'olderthandays' , empty removed. default behaviour of script leave empty folders behind.  .example     delete-oldfiles -target "\\grouphc.net\bnl\departments\brussels\cbr\share\target" -olderthandays "10"     delete-oldfiles "\\grouphc.net\bnl\departments\brussels\cbr\share\target" "10"     delete-oldfiles "e:\departments\cbr\share\target" "10"  description:     deletes files older 10 days in target folder , of subfolders , write success , failure log.  .example     delete-oldfiles "\\grouphc.net\bnl\departments\brussels\cbr\share\target" "10" -cleanfolders  description:     deletes files older 10 days , empty folders in target folder , of subfolders , write success , failure log.  .notes     requirements remote server:     powershell 2.0     admin run 'set-executionpolicy remotesigned'     amdin run 'enable-wsmancredssp -role server -force'      requirements script server:     enable-wsmancredssp -role client -delegatecomputer *.grouphc.net -force      changelog     2014/05/06 script born      2014/05/07 added total runtime of script logfile     2014/05/12 added timestamps, fail , success messages logfile     2014/05/13 added powershell 2.0 compatibility windows server 2003     2014/05/13 added remove empty directories     2014/05/14 added comment section on top     2014/05/15 added parameters pass variables $target , $olderthandays     2014/05/19 added csv input file easy management     2014/05/20 created function delete-oldfiles , delete-oldfolders     2014/05/22 rewrote script calling functions     2014/05/23 added switch -cleanfolders     2014/06/02 added password authentication double hop     2014/06/03 added loging capability different files timestamps  .author     me #> [cmdletbinding(supportsshouldprocess=$true)] # add -whatif support dry run param(     [parameter(mandatory=$true,position=1)]     [validatescript({test-path $_})]     [string]$target,     [parameter(mandatory=$true,position=2)]     [int]$olderthandays,     [parameter(mandatory=$false,position=3)]     [string]$server,     [switch]$cleanfolders       )  #__________________________________________________________________________________________________________________________________ # create logfile correct name # gets date , reformats used in log filename $tempdate = (get-date).tostring("dd-mm-yyyy") # reformats $target can used in log filename $tempfolderpath = $target -replace '\\','_' $tempfolderpath = $tempfolderpath -replace ':','' $tempfolderpath = $tempfolderpath -replace ' ','' # combines date , path scanned log filename $script:logfile = "\\deustheidit02\log\scheduled task\auto_clean\$server - $tempfolderpath - $tempdate.log"  #__________________________________________________________________________________________________________________________________ # check version of powershell if ($psversiontable.psversion.major -ge "3") {      # powershell 3+ remove files older (faster)     get-childitem -path $target -recurse -file |      where-object { $_.creationtime -lt (get-date).adddays(-$olderthandays) } |           foreach {         $item = $_.fullname         remove-item $item -recurse -force -erroraction silentlycontinue              # log succes/failure             $timestamp = (get-date).toshortdatestring()+" | "+(get-date).tolongtimestring()                   if (test-path $item) {                  "$timestamp | failled: $server $item (in use)"             }              else {                  "$timestamp | removed: $server $item"              }           } | tee-object $logfile -append }  else {                     # powershell 2 remove files older     get-childitem -path $target -recurse |      where-object { !$_.psiscontainer -and $_.creationtime -lt (get-date).adddays(-$olderthandays) } |           foreach {         $item = $_.fullname         remove-item $item -recurse -force -erroraction silentlycontinue              # log succes/failure             $timestamp = (get-date).toshortdatestring()+" | "+(get-date).tolongtimestring()                if (test-path $item) {                  write-host "$timestamp | failled: $server $item (in use)"                "$timestamp | failled: $server $item (in use)"             }              else {                    write-host "$timestamp | removed: $server $item"                  "$timestamp | removed: $server $item"             }           } | out-file $logfile -append }   #__________________________________________________________________________________________________________________________________     # switch -cleanfolders deletes empty folders older x days     if ($cleanfolders) {          # check version of powershell         if ($psversiontable.psversion.major -ge "3") {              # powershell 3+ remove empty folders older (faster)             get-childitem -path $target -recurse -force -directory  -erroraction silentlycontinue |             where-object { $_.creationtime -lt (get-date).adddays(-$olderthandays) } |              where-object { (get-childitem -path $_.fullname -recurse -force -file) -eq $null } |                   foreach {                 $item = $_.fullname                 remove-item $item -recurse -force -erroraction silentlycontinue                      # log succes/failure                     $timestamp = (get-date).toshortdatestring()+" | "+(get-date).tolongtimestring()                     if (test-path $item) {                          "$timestamp | failled: $server $item (in use)"                      }                      else {                          "$timestamp | removed: $server $item"                      }                   } | tee-object $logfile -append         }               else {              # powershell 2 remove empty folders older              get-childitem -path $target -recurse -force -erroraction silentlycontinue |              where-object { $_.psiscontainer -and (get-childitem -path $_.fullname -recurse -force | where-object { !$_.psiscontainer }) -eq $null } |              where-object { $_.creationtime -lt (get-date).adddays(-$olderthandays) } |                   foreach {                 $item = $_.fullname                 remove-item $item -recurse -force -erroraction silentlycontinue                      # log succes/failure                     $timestamp = (get-date).toshortdatestring()+" | "+(get-date).tolongtimestring()                           if (test-path $item) {                          write-host "$timestamp | failled: $server $item (in use)"                         "$timestamp | failled: $server $item (in use)"                      }                      else {                          write-host "$timestamp | removed: $server $item"                         "$timestamp | removed: $server $item"                      }                   } |  out-file $logfile -append          }      } } }  #__________________________________________________________________________________________________________________________________ # read input file , ignore lines starting # $file = (import-csv -path $importfile -header "server", "target", "olderthandays" | { $_.server -notmatch "#" } )  #__________________________________________________________________________________________________________________________________  # if unc path provided run script locally else wil run on remote server job foreach ($_ in $file) {          # set easy names         $server = $_.server         $target = $_.target         $olderthandays = $_.olderthandays          if ($server -eq "unc")         {             write-host "unc path detected: $target, $olderthandays" -foregroundcolor yellow             start-job -scriptblock {param($target, $olderthandays, $server) delete-oldfiles $target $olderthandays $server} -initializationscript $jobfunc -argumentlist ($target, $olderthandays, $server) -name delfiles             # remove empty folders to: delete-oldfiles $target $olderthandays $server -cleanfolders         }         else         {             write-host "local path detected: $server, $target, $olderthandays" -foregroundcolor cyan              $credentials = new-object system.management.automation.pscredential -argumentlist $username,$password              $credentials = new-object system.management.automation.pscredential -argumentlist $username,$password             invoke-command -computername "$server.grouphc.net" -authentication credssp -credential $credentials -scriptblock {$jobfunc; delete-oldfiles $target $olderthandays $server} -argumentlist ($target, $olderthandays, $server) #-asjob -jobname delfiles              #invoke-command -computername "$server.grouphc.net" -authentication credssp -credential $credentials -scriptblock {delete-oldfiles $args[0] $args[1] $args[2] -cleanfolders:$args[3]} -argumentlist ($target, $olderthandays, $server, $true) -asjob -jobname delfiles             # invoke-command -computername "$server.grouphc.net" -authentication credssp -credential $credentials -scriptblock ${function:delete-oldfiles} -argumentlist ($target, $olderthandays, $server)              #invoke-command -computername "$server.grouphc.net" -authentication credssp -credential $credentials -scriptblock ${function:delete-oldfiles} -argumentlist ($target, $olderthandays, $server) -asjob -jobname delfiles             #invoke-command -computername "$server.grouphc.net" -authentication credssp -credential $credentials -scriptblock {delete-oldfiles $target, $olderthandays, $server} -argumentlist ($target, $olderthandays, $server) #-asjob -jobname delfiles             #invoke-command -computername "$server.grouphc.net" -authentication credssp -credential $credentials -scriptblock {function:delete-oldfiles} -argumentlist ($target, $olderthandays, $server) #-asjob -jobname delfiles             #invoke-command -computername "$server.grouphc.net" -authentication credssp -credential $credentials -scriptblock ${function:delete-oldfiles} -argumentlist ($target, $olderthandays, $server)             #invoke-command -computername "$server.grouphc.net" -authentication credssp -credential $credentials -scriptblock {$jobfunc} -argumentlist ($target, $olderthandays, $server) -asjob -jobname delfiles             #invoke-command -computername "$server.grouphc.net" -authentication credssp -credential $credentials -scriptblock ${function:delete-oldfiles} -argumentlist ($target, $olderthandays, $server)         }   } 

thank help.

try this:

$sb = {   function delete-oldfiles {     #...   }    delete-oldfiles $args[0] $args[1] $args[2] -cleanfolders:$args[3] }  invoke-command -computername $server -authentication credssp `   -credential $credentials -scriptblock $sb `   -argumentlist ($target, $olderthandays, $server, $true) 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -