marcimarc85
Goto Top

Powershell zweites Script mit Parameter in relativem Pfad starten

Hallo ,

Ich habe ein Powershell Script, welches unter D: liegt.

Dieses soll ein weiteres Script starten, welches unter D:\utils\update\Powershell liegt un die variable $var1 mitgeben

Ich habe folgendes versucht:

$PSScriptRoot+"utils\update\Powershell\update.ps1 $var1"  

Das gibt nur den gesamten Pfad aus:

D:\utils\update\Powershell\update.ps1 4.5.123.3
.


und

Start-Process -Filepath  $PSScriptRoot+"utils\update\Powershell\update.ps1 "+$var1  


Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
At D:\Untitled2.ps1:2 char:1
+ Start-Process -Filepath  $PSScriptRoot+"utils\update\Powershell\update.ps1  ...  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Content-Key: 72901046737

Url: https://administrator.de/contentid/72901046737

Printed on: June 11, 2024 at 15:06 o'clock

Member: CamelCase
CamelCase May 16, 2024 updated at 09:10:21 (UTC)
Goto Top
Moin,

versuch mal

Start-Process powershell.exe -ArgumentList "$(Join-Path $PSScriptRoot "utils\update\Powershell\update.ps1") $var1"  
Member: MarciMarc85
MarciMarc85 May 16, 2024 at 09:16:59 (UTC)
Goto Top
Damit wird das Script schonmal gestartet, aber die Variable nicht mitgegeben
Member: hempel
Solution hempel May 16, 2024 updated at 09:25:06 (UTC)
Goto Top
$var1 = "4.5.123.3"
&"$PSScriptRoot\utils\update\Powershell\update.ps1" $var1
Gruß
Member: MarciMarc85
MarciMarc85 May 16, 2024 at 09:25:16 (UTC)
Goto Top
Danke! das wars!
Member: MarciMarc85
MarciMarc85 May 30, 2024 updated at 07:18:58 (UTC)
Goto Top
@hempel

Ich müsste nun noch eine zweite Variable mitgeben:

$new_version = "hierstehtdiezielversion"
$ADMIN_PASSWORD = "hierstehteinpwd"

was noch nicht funktioniert. Das ZielScript sieht folgendermaßen aus.:

param (
[Parameter(Mandatory=$true)][String]$new_version,
[Parameter(Mandatory=$true)][String]$ADMIN_PASSWORD
)


#Email Daten festlegen
$abs="abs@irgendwo.de"  
$empf="<empf@irgendwo.de>"  
$sub="Version $new_version"  
$smtp="192.168.8.20"  

#Gruss festlegen
$uhrzeit = get-date -Format "HH"  
    if ($uhrzeit -lt 10) { 
       $gruss = "Guten Morgen";   
       } 
elseif ($uhrzeit -ge 10 -and $uhrzeit -lt 18){ 
       $gruss = "Hallo";   
       } 
  else { 
       $gruss = "Guten Abend";   
       }



$RootPath = Split-Path $PSScriptRoot -Parent
$serverlist = "D:\Update_Scripts\serverlist.ini"  
$computers = get-content $serverlist -encoding UTF8 


$body = ""  
$body += "$gruss `n`n" + "die folgenden Systeme sind auf $new_version aktualisiert:`n`n"  

    
$update_job = {

Param(
[string]$computer,
[string]$RootPath,
[string]$new_version,
[string]$ADMIN_PASSWORD
)
 
 $command = "$RootPath\PsExec.exe"  
 $scriptpath ="D:\software\bin\powershell_tools\update_wizard_silent.ps1"  
 & $command "\\$computer" -u "$computer\Administrator" -p $ADMIN_PASSWORD -s -i "powershell" $scriptpath $new_version       
 Write-host "Skript auf PC $computer erfolgreich" -ForegroundColor green  
   }

#Remove all jobs
Get-Job | Remove-Job
$MaxThreads = 8
#Start the jobs. Max 8 jobs running simultaneously.
Foreach ($computer in $computers) {

    While ($(Get-Job -state running).count -ge $MaxThreads){
        Start-Sleep -Milliseconds 3
    }
    
    Start-Job -Name $computer -Scriptblock $update_job -ArgumentList $computer,$RootPath,$new_version
    $body += "            - $computer`n"      
}

#Wait for all jobs to finish.
While ($(Get-Job -State Running).count -gt 0){
    start-sleep 1
}
#Get information from each job.
foreach($job in Get-Job){
    $info= Receive-Job -Id ($job.Id)
}

#Remove all jobs created.
Get-Job | Remove-Job

$body += "`nGruß `n`n" + "das Update-Script"  


Send-MailMessage -From $abs -To $empf -Subject $sub -Body $body -Encoding ([System.Text.Encoding]::UTF8) -SmtpServer $smtp

Die Variable $ADMIN_PASSWORD bleibt im Zielscript aber immer leer.
Member: hempel
Solution hempel May 30, 2024 updated at 07:35:34 (UTC)
Goto Top
Hast hier vergessen das Passwort an den Skriptblock als Argument zu übergeben
Start-Job -Name $computer -Scriptblock $update_job -ArgumentList $computer,$RootPath,$new_version,$ADMIN_PASSWORD
Member: MarciMarc85
MarciMarc85 May 30, 2024 at 07:40:10 (UTC)
Goto Top
Oh man. Tatsächlich. Hab ich komplett ignoriert.

Vielen dank ! face-smile