دنبال کننده ها

۱۳۹۶ اسفند ۱۰, پنجشنبه

Powershell function to return object

[ad_1]



I want to have in a .ps1 file some code that creates a PSSession that can be used in other .ps1 scripts (in order to avoid code duplication).



At first I thought i need a function that creates a PSSession and returns it but I am confused about how they the function outputs work.



Here is my function:



function newRemoteSession

param([string]$ipAddress)

$accountName = 'admin'
$accountPassword = 'admin'
$accountPasswordSecure = ConvertTo-SecureString $accountPassword -AsPlainText -Force
$accountCredential = New-Object System.Management.Automation.PSCredential ($accountName, $accountPasswordSecure)

Try

$remoteSession = New-PSSession -ComputerName $ipAddress -UseSSL -Credential $accountCredential -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -ErrorAction Stop

Catch [System.Management.Automation.RuntimeException]#PSRemotingTransportException

Write-Host 'Could not connect with default credentials. Please enter credentials...'
$remoteSession = New-PSSession -ComputerName $ipAddress -UseSSL -Credential (Get-Credential) -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -ErrorAction Stop
Break


return $remoteSession



However when I call $s = newRemoteSession(192.168.1.10), $s is empty.




[ad_2]

لینک منبع