gpt4 book ai didi

功能和 powershell 远程处理

转载 作者:行者123 更新时间:2023-12-01 00:51:34 25 4
gpt4 key购买 nike

我有一个运行良好的脚本,但我想提高我的 powershell 知识,并想知道是否有更简单的方法来做到这一点.....

我的脚本的一部分连接到服务器并在其上提取文件和大小列表并将其导出到 csv。

我找到了一个函数 (Exportcsv),它允许我使用早期版本的 powershell 附加到 csv。

目前,我使用 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

安迪

最佳答案

不,除了您已经在做的事情之外,没有一种直接的方法可以将功能传递给远程计算机。 :-) 但是,您可以将所有脚本放在一个文件 (dirsize.ps1) 中,然后使用 FilePath 参数将其传递给 Invoke-Command:

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

该文件将被复制到远程计算机并执行。

关于功能和 powershell 远程处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com