-1

我在尝试了解如何在脚本块上集成功能时遇到问题。我需要脚本块的原因是因为我想在 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 {
    }
}
4

1 回答 1

0

您可以将函数保存到 PS 脚本并从中创建脚本块。

$script = Get-Content -Path "C:\Folder\YourFunctionScript.ps1"
$ScriptBlock = [Scriptblock]::Create($script)

您可以简单地通过调用它invoke-command

Invoke-Command -ScriptBlock $ScriptBlock
于 2017-01-10T15:30:34.483 回答