Déclarer une fonction locale dans une remote session
Bonjour,
Je cherche à faire en sorte qu'une fonction déclarée dans un script et ouvrant une remote session puisse être connue dans cette même remote-session sans avoir à faire d'import local d'un module.
Autant, je trouve facilement comment passer une variable. Ca c'est OK.
Autant, pour les fonctions je trouve des bouts de codes incomplets et que je n'arrive pas à adapter à mon cas.
Je souhaite pouvoir appeler la fonction et utiliser la fonction Write-Log dans le ScriptBlock executé dans la remote-session.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| $log_dir = $env:USERPROFILE + '\Desktop\'
$log_name = 'Remote-idm_log.csv'
$log_FullName = $log_dir + $log_name
Function Write-Log
{
Param
(
[Parameter(Mandatory=$False)][ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG","JOB")][String]$level = "INFO",
[Parameter(Mandatory=$True)][string]$data,
[Parameter(Mandatory=$True)][string]$logfile
)
$stamp = (Get-Date).toString("dd/MM/yyyy;HH:mm:ss")
$data = "$stamp;$Level;$data"
if ( $data ) { Write-Host $data }
If ( !( Test-Path $log ) )
{
New-Item -Path $log_dir -Name $log_name -ItemType "file" | Out-Null
}
else
{
if ( $data ) { Add-Content $logfile -Value $data }
else { Add-Content $logfile -Value "$stamp;$Level;NODATA" }
}
}
$WriteLog_Remote = Get-Item 'Function:\Write-Log'
$url_file = $env:TMP + '\url.txt'
Start-Process notepad.exe -ArgumentList $url_file -Wait
$link_list = Get-Content $url_file
foreach ( $link in $link_list )
{
if ( $link )
{
$PSSession_id = New-PSSession -ComputerName 'NAS'
Invoke-Command -Session $PSSession_id -ScriptBlock {
param( $link, $WriteLog_Remote, $logFile )
[ScriptBlock]::Create($using:WriteLog_remote)
$arguments = '/s' + ' /d ' + $using:link + ' /p E:\DOWNLOAD\ /n /q'
Write-Host Downloading $using:link -ForegroundColor Green
&$using:WriteLog_Remote -level 'INFO' -data $using:link -logfile $using:logFile
$process = Start-Process IDMan.exe -ArgumentList $arguments -NoNewWindow -PassThru
$process.WaitForExit()
} -ArgumentList $link, $WriteLog_Remote, $log_FullName
Remove-PSSession $PSSession_id
}
} |
Je vous remercie par avance pour vos lumières car clairement j'avance à tâtons sans trop comprendre ce qu'il se passe et ça m'agace :p