-1

I am having issues trying to understand how to integrate a function on a scriptblock. The reason why I need the scriptblock is because I want to run a powershell script in orchestrator unless someone can help me how can I run a working customized function in orchestrator. The function I want to run I got it from another site but I changed the name of the variables.

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 {
    }
}
soundslikeodd
  • 1,078
  • 3
  • 19
  • 32

1 Answers1

0

You can save your function to a PS script and create a script block from it.

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

You can simply invoke it thru invoke-command

Invoke-Command -ScriptBlock $ScriptBlock
Prageeth Saravanan
  • 1,053
  • 1
  • 8
  • 24
  • If I understood it correct, the OP want to integrate this function to a script block. Now, seeing back with your comment it could also be perceived as he want to integrate his function to an existing script block ! Let's wait for more clarity ! – Prageeth Saravanan Jan 10 '17 at 15:38
  • Prageet this is what i have used: $Servers = 'server02' $script = Get-Content -Path "D:\Scripts\Test-RDP.ps1" $ScriptBlock = [Scriptblock]::Create($script) Invoke-Command -ScriptBlock $ScriptBlock Get-RDPStatus -ComputerName $Servers | Export-Csv c:\temp\Post-Patching-Runbook.csv Not sure if this is correct. – Jose Antonio Chan Jan 10 '17 at 19:06
  • Where do you want to call the Get-RDPStatus method ? What is it you are really trying to do..? – Prageeth Saravanan Jan 11 '17 at 13:32
  • This link explain how to run a powershell script in System Center Orchestrator which is what im trying to do. https://automys.com/library/asset/powershell-system-center-orchestrator-practice-template Im having trouble trying to run run that function. – Jose Antonio Chan Jan 11 '17 at 13:55