Перезагрузите и выполните скрипт Power Shell - PullRequest
0 голосов
/ 23 января 2020

У меня есть два сценария Power Shell Script1.ps1 и Script2.ps1. Script1.ps1 переименует хост и выполнит перезагрузку. после перезагрузки я хочу иметь возможность автоматически подключаться к компьютеру, который требует учетные данные, а затем выполнить свой скрипт2.ps1, может кто-нибудь предоставить мне решение для этого. я прошел через похожие вопросы и посты, в которых говорится об использовании рабочего процесса powershell или использовании реестра HKLM \ Software \ Microsoft \ Windows \ CurrentVersion \ RunOnce. но я новичок в этом и не разбираюсь в достаточном количестве деталей ... спасибо, если кто-нибудь может дать мне рабочую команду с примером. или какие команды добавить в script1.ps1, чтобы после перезагрузки выполнялся Script2.ps1

Script1.ps1:

#read newservername and change the computer name
$computerName = Get-WmiObject Win32_ComputerSystem
$name = Read-Host -Prompt "Please Enter the New Host-Name you want to use."
$computername.Rename($name)
[System.Windows.MessageBox]::Show('Please wait System is rebooting in 5 seconds to make the changes')
start-sleep 5
restart-Computer

Script2.ps1 :

 # ==== Step 1:Run sql script to change hostname on the Database
    Invoke-Sqlcmd -ServerInstance 'localhost\MyDB' -Database 'LFG' -InputFile 
 "C:\Users\Administrator\Desktop\ChangingHostname\2- Changehostname-DB.sql" | Out-File -FilePath 
 "C:\Users\Administrator\Desktop\ChangingHostname\2- Changehostname-DB.txt"
    Restart-Service -Force 'MSSQL$CARDIO'
    Start-Service -Name 'SQLAgent$CARDIO'

    #==== Step 2:Get the old server name from config file and store it.
    $xml = [xml](get-content E:\MARDAS\AdminTools\bin\khc.lms.common.config.config)
    $getValue = $xml.SelectNodes("/configuration/khc.lms.common.remoting/item")| foreach { $_.value}
    $oldServerName= $getValue.Get(0)
    write $oldServerName

    #==== Step 3:replace old server name with New servername of the System.
    $newServerName = $env:COMPUTERNAME
    Write-Host $newServerName
    Get-ChildItem -Path D:\MARDAS\* -Include *.ps,*.ps1,*.config,*.BAT,*.JSON,*.js,*.sql,*.htm -Recurse | ForEach {
      (Get-Content $_ | ForEach {$_ -replace "$oldServerName", "$newServerName"})| Set-Content -Force $_
    }
    Write-Host "Completed renaming hostname...."

    #==== Step 4:Import certificate into certificate store
    $myPassword = Read-Host "Enter the password of certficate" -AsSecureString
    Import-PfxCertificate -FilePath C:\Users\Administrator\Desktop\ChangingHostname\SERVERS.lm.ked.me.com.pfx -CertStoreLocation Cert:\LocalMachine\My -Password $myPassword
...