簡體   English   中英

Scriptblock 內的函數無法理解

[英]function inside Scriptblock unable to understand

我在嘗試了解如何在腳本塊上集成函數時遇到問題。 我需要 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 {
    }
}

您可以將函數保存到 PS 腳本並從中創建腳本塊。

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

您可以簡單地通過invoke-command調用它

Invoke-Command -ScriptBlock $ScriptBlock

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM