简体   繁体   English

功能和powershell远程处理

[英]Functions & powershell remoting

I have a script that works fine but I want to improve my powershell knowledge and would like to know if there is an easier way to do this..... 我有一个工作正常的脚本,但我想提高我的powershell知识,并想知道是否有更简单的方法来做到这一点.....

Part of my script connects to a sever and pulls bak a list of files and sizes on it and exports it to a csv. 我的部分脚本连接到服务器,并在其上提取bak文件和大小列表,并将其导出到csv。

I have found a function (Exportcsv) that allows me to append to the csv with earlier versions of powershell. 我找到了一个函数(Exportcsv),它允许我使用早期版本的powershell附加到csv。

At the moment I use the invoke-command to remote to each server and the run the script in the script block but this means adding the function each time. 目前,我使用invoke-command远程访问每个服务器,并在脚本块中运行脚本,但这意味着每次都要添加该功能。 So I have the function in my script but then have to repeat it for each server I connect to so it will run remotely 所以我在我的脚本中有这个功能,但是我必须为我连接的每个服务器重复它,所以它将远程运行

Is there any way to pass the local function to the remote server so I don't have to add to each invoke command. 有没有办法将本地函数传递给远程服务器,所以我不必添加到每个调用命令。

Invoke-Command –ComputerName server –ScriptBlock {
$wfile = "d:\folder\directorysize_H.csv"
$xfile = "d:\folder\directorysize_F.csv"

function ExportCSV {
[CmdletBinding(DefaultParameterSetName='Delimiter',
  SupportsShouldProcess=$true, ConfirmImpact='Medium')]
param(
 [Parameter(Mandatory=$true, ValueFromPipeline=$true,
           ValueFromPipelineByPropertyName=$true)]
 [System.Management.Automation.PSObject]
 ${InputObject},

 [Parameter(Mandatory=$true, Position=0)]
 [Alias('PSPath')]
 [System.String]
 ${Path},

 #region -Append
 [Switch]
 ${Append},
 #endregion 

 [Switch]
 ${Force},

 [Switch]
 ${NoClobber},

 [ValidateSet('Unicode','UTF7','UTF8','ASCII','UTF32',
                  'BigEndianUnicode','Default','OEM')]
 [System.String]
 ${Encoding},

 [Parameter(ParameterSetName='Delimiter', Position=1)]
 [ValidateNotNull()]
 [System.Char]
 ${Delimiter},

 [Parameter(ParameterSetName='UseCulture')]
 [Switch]
 ${UseCulture},

 [Alias('NTI')]
 [Switch]
 ${NoTypeInformation})

begin
{
 # This variable will tell us whether we actually need to append
 # to existing file
 $AppendMode = $false

 try {
  $outBuffer = $null
  if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
  {
      $PSBoundParameters['OutBuffer'] = 1
  }
  $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Export-Csv',
    [System.Management.Automation.CommandTypes]::Cmdlet)


 #String variable to become the target command line
 $scriptCmdPipeline = ''

 # Add new parameter handling
 #region Process and remove the Append parameter if it is present
 if ($Append) {

  $PSBoundParameters.Remove('Append') | Out-Null

  if ($Path) {
   if (Test-Path $Path) {        
    # Need to construct new command line
    $AppendMode = $true

    if ($Encoding.Length -eq 0) {
     # ASCII is default encoding for Export-CSV
     $Encoding = 'ASCII'
    }

    # For Append we use ConvertTo-CSV instead of Export
    $scriptCmdPipeline += 'ConvertTo-Csv -NoTypeInformation '

    # Inherit other CSV convertion parameters
    if ( $UseCulture ) {
     $scriptCmdPipeline += ' -UseCulture '
    }
    if ( $Delimiter ) {
     $scriptCmdPipeline += " -Delimiter '$Delimiter' "
    } 

    # Skip the first line (the one with the property names) 
    $scriptCmdPipeline += ' | Foreach-Object {$start=$true}'
    $scriptCmdPipeline += '{if ($start) {$start=$false} else {$_}} '

    # Add file output
    $scriptCmdPipeline += " | Out-File -FilePath '$Path'"
    $scriptCmdPipeline += " -Encoding '$Encoding' -Append "

    if ($Force) {
     $scriptCmdPipeline += ' -Force'
    }

    if ($NoClobber) {
     $scriptCmdPipeline += ' -NoClobber'
    }   
   }
  }
 } 



 $scriptCmd = {& $wrappedCmd @PSBoundParameters }

 if ( $AppendMode ) {
  # redefine command line
  $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock(
      $scriptCmdPipeline
    )
 } else {
  # execute Export-CSV as we got it because
  # either -Append is missing or file does not exist
  $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock(
      [string]$scriptCmd
    )
 }

 # standard pipeline initialization
 $steppablePipeline = $scriptCmd.GetSteppablePipeline(
        $myInvocation.CommandOrigin)
 $steppablePipeline.Begin($PSCmdlet)

 } catch {
   throw
 }

}

process
{
  try {
      $steppablePipeline.Process($_)
  } catch {
      throw
  }
}

end
{
  try {
      $steppablePipeline.End()
  } catch {
      throw
  }
}
}

Write-Host "Removing old files from xxx"

If (Test-Path $wfile){
            Remove-Item $wfile
            } 

If (Test-Path $xfile){
            Remove-Item $xfile
            } 

write-host "Getting _f details"
get-childitem \\server\F$ -recurse |select directory, name, length|exportcsv $xfile -append -noclobber -notypeinformation 
write-host "Getting _H details"
get-childitem \\server\H$ -recurse |select directory, name, length|exportcsv $wfile -append -noclobber -notypeinformation    
}

TIA TIA

Andy 安迪

No there isn't a straightforward way to pass the function to the remote computers, well other than what you are already doing. 没有一种直接的方法可以将功能传递给远程计算机,除了你已经在做的事情之外。 :-) However you can put all that script in a file (dirsize.ps1) and pass that to Invoke-Command using the FilePath parameter: :-)但是你可以将所有脚本放在一个文件(dirsize.ps1)中,并使用FilePath参数将其传递给Invoke-Command:

Invoke-Command –ComputerName server –FilePath .\dirsize.ps1

The file will be copied to the remote computers and executed. 该文件将被复制到远程计算机并执行。

try this: 试试这个:

#Local Function
function Get-Bits
{
    Get-Service -Name BITS
}

Invoke-Command -ComputerName RemoteServer -ScriptBlock ${function:Get-Bits} 

personally i put all my functions in a psm1 file in other words a module on a network share and import the module while in the remote session. 我个人将所有功能都放在psm1文件中,换句话说就是网络共享上的模块,并在远程会话中导入模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM