$DriveLetter = "S:" $UncPath = "\\コンピューター名\共有名" $UserName = "ユーザー名" $Password = "パスワード" $SetupPath = "$DriveLetter\パッケージフォルダー\Setup.exe" $ArgumentList = "/Archive `"$env:USERPROFILE\Desktop\SetROBOlog_$env:COMPUTERNAME.zip`"" try { cmd /c "net use S: $UncPath /user:$UserName $Password /persistent:no" if ($LASTEXITCODE -ne 0) { throw "ネットワークドライブの接続に失敗しました。" } # デスクトップシェルの起動とWindows Hello for Businessセットアップ完了を待つ do { Start-Sleep -Milliseconds 500 $explorer = Get-Process explorer -ErrorAction SilentlyContinue } until ($explorer -and $explorer.MainWindowHandle -ne 0) # さらに60秒待機 Start-Sleep 60 # Setup.exe 実行 $process = Start-Process -FilePath $SetupPath -ArgumentList $ArgumentList -Wait -PassThru if ($process.ExitCode -ne 0) { throw "Setup.exe がエラー終了しました。(ExitCode=$($process.ExitCode))" } # 成功 exit 0 } catch { Write-Error $_ exit 1 } finally { # 必ず切断 cmd /c "net use $DriveLetter /delete /y" | Out-Null }