我在尝试理解如何在scriptblock上集成函数时遇到问题。我需要scriptblock的原因是因为我想在orchestrator中运行一个powershell脚本,除非有人可以帮助我如何在orchestrator中运行一个有效的自定义函数。我想要运行的功能是从其他站点获取的,但我更改了变量的名称。
Function Get-RDPStatus {
param (
[CmdletBinding()]
[string[]]$ComputerName = $env:COMPUTERNAME
)
begin {
$SelectHash = @{
'Property' = @('Name','ADObject','DNSEntry','PingResponse','RDPConnection')
}
}
process {
foreach ($CurrentComputer in $ComputerName) {
# Create new Hash
$HashProps = @{
'Name' = $CurrentComputer
'ADObject' = $false
'DNSEntry' = $false
'RDPConnection' = $false
'PingResponse' = $false
}
# Perform Checks
switch ($true)
{
{([adsisearcher]"samaccountname=$CurrentComputer`$").findone()} {$HashProps.ADObject = $true}
{$(try {[system.net.dns]::gethostentry($CurrentComputer)} catch {})} {$HashProps.DNSEntry = $true}
{$(try {$socket = New-Object Net.Sockets.TcpClient($CurrentComputer, 3389);if ($socket.Connected) {$true};$socket.Close()} catch {})} {$HashProps.RDPConnection = $true}
{Test-Connection -ComputerName $CurrentComputer -Quiet -Count 1} {$HashProps.PingResponse = $true}
Default {}
}
# Output object
New-Object -TypeName 'PSCustomObject' -Property $HashProps | Select-Object @SelectHash
}
}
end {
}
}
答案 0 :(得分:0)
您可以将功能保存到PS脚本并从中创建脚本块。
$script = Get-Content -Path "C:\Folder\YourFunctionScript.ps1"
$ScriptBlock = [Scriptblock]::Create($script)
您可以通过invoke-command
Invoke-Command -ScriptBlock $ScriptBlock