Shi*_* JA 4 powershell iis-7 powershell-2.0
我试图在提供凭据后让 PowerShell 在远程计算机上停止并启动 AppPool。
启动应用程序池的函数:
Function fnStartApplicationPool([string]$appPoolName)
{
import-module WebAdministration
if((Get-WebAppPoolState $appPoolName).Value -ne 'Started')
{
Start-WebAppPool -Name $appPoolName
}
}
Run Code Online (Sandbox Code Playgroud)
停止应用程序池的函数:
Function fnStopApplicationPool([string]$appPoolName)
{
import-module WebAdministration
if((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped')
{
Stop-WebAppPool -Name $appPoolName
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码不起作用:
if ($pathback -eq $false)
{
#Copying Data from Source to Destination
copy-Item -Recurse $backupsrc -Destination $backupdes
write-host "Backup Successful"
#Validating the apppool value
import-module WebAdministration
if((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped')
{
#Stop apppool
Stop-WebAppPool -Name $appPoolName
write-host "AppPool Stopped Successfully"
}
#Copying Data from Source to Destination
#Start apppool
Start-WebAppPool -Name $appPoolName
write-host "AppPool Started Sucessfully"
cd c:\
}
Run Code Online (Sandbox Code Playgroud)
要远程运行脚本,您必须确保启用PS-Remoting 。
\n\n右键单击 Windows PowerShell 快捷方式并选择以管理员身份运行,以管理员身份启动 Windows PowerShell 。
默认情况下,WinRM服务配置为手动启动。您必须将启动类型更改为自动,并在要使用的每台计算机上启动该服务。在 PowerShell 提示符下,您可以使用以下命令验证 WinRM 服务是否正在运行: \n get-service winrm \n如果该服务未运行,请通过Start-Service winrm使其运行
\n\n\n启用-PSRemoting \xe2\x80\x93force
\n
\n\n\nwinrm s winrm/config/client \'@{TrustedHosts="RemoteComputer"}\'
\n
\n\n\nwinrm快速配置
\n
此命令分析并配置 WinRM 服务。
\n\n就您而言,您必须在 ServerB 中执行所有这些操作,因为 ServerB 必须信任 ServerA。
\n\n完成这些操作后,您可以从 ServerA 运行以下脚本。我在脚本本身中添加了一些要点供您参考。您可以根据您的要求更改占位符。
\n\n# Embedding the password in the script.\n# If you do not have a domain creds, then use the username and password directly.\n\n$MyDomain=\'MyDomain\' ;\n$MyClearTextUsername=\'Username\' ;\n$MyClearTextPassword=\'Password\' ;\n$MyUsernameDomain=$MyDomain+\'\\\'+$MyClearTextUsername;\n$SecurePassword=Convertto-SecureString \xe2\x80\x93String $MyClearTextPassword \xe2\x80\x93AsPlainText \xe2\x80\x93force ;\n$MyCreds=New-object System.Management.Automation.PSCredential $MyUsernameDomain,$SecurePassword ;\n\n# Placing the script under a ScriptBlock\n$MyScriptblock={param($appPoolName,$pathback)\n# Since you have mentioned that it is working fine locally, I am not checking this part. Assuming its fine.\n# Defining the functions as Global. So that you can use it anywhere although I am putting in the scriptblock.\n# Make sure the module is present in the remote system. It should be cause you have already mentioned it is working fine when you are running from that system.\n Function fnStartApplicationPool([string]$appPoolName)\n {\n import-module WebAdministration\n if((Get-WebAppPoolState $appPoolName).Value -ne \'Started\')\n {\n Start-WebAppPool -Name $appPoolName\n }\n }\n Function fnStopApplicationPool([string]$appPoolName)\n {\n import-module WebAdministration\n if((Get-WebAppPoolState $appPoolName).Value -ne \'Stopped\')\n {\n Stop-WebAppPool -Name $appPoolName\n }\n }\n if ($pathback -eq $false) \n { \n #Copying Data from Source to Destination\n copy-Item -Recurse $backupsrc -Destination $backupdes \n write-host "Backup Successful" \n\n #Validating the apppool value\n import-module WebAdministration \n if((Get-WebAppPoolState $appPoolName).Value -ne \'Stopped\') \n {\n #Stop apppool \n Stop-WebAppPool -Name $appPoolName \n write-host "AppPool Stopped Successfully"\n }\n #Copying Data from Source to Destination\n\n #Start apppool\n Start-WebAppPool -Name $appPoolName \n write-host "AppPool Started Sucessfully"\n cd c:\\ \n } \n\n }\n\n# As you want to Stop the App pool in Server B from Server A.\n# run the script under server A and provide the Server B creds\n\n$result=Invoke-Command -ComputerName \'ServerB\' -Credential $MyCreds -ScriptBlock $MyScriptblock -ArgumentList $appPoolName,$pathback ;\n$result ;\nRun Code Online (Sandbox Code Playgroud)\n\n如果您对答案感到满意,请随意喜欢并接受这个答案,这也将对其他人有所帮助。
\n