重新启动并执行Power Shell脚本

时间:2020-01-22 21:10:02

标签: powershell-2.0 powershell-3.0 powershell-4.0 powershell-remoting powershell-5.0

我有两个Power Shell脚本Script1.ps1和Script2.ps1。 Script1.ps1将重命名主机并重新启动。重启后,我希望能够自动登录到需要凭据的计算机,然后执行我的script2.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

0 个答案:

没有答案