I need to reset iis site using team city build.
I have decided to use PowerShell as I have been using it for everything else to do with the build.
When I have attempted to run this under my normal credentials, it did not work and I got error:
System.InvalidOperationException: Process should have elevated status to access IIS configuration data.
at Microsoft.IIs.PowerShell.Provider.ConfigurationPr
ovider.Start(ProviderInfo providerInfo)
at System.Management.Automation.SessionStateInternal
.NewProvider(ProviderInfo provider)
at System.Management.Automation.SessionStateInternal
.AddProvider(Type implementingType, String name,
String helpFileName, PSSnapInInfo psSnapIn,
PSModuleInfo module)
Not to be beaten, I have proceeded on trip how to run this with elevated permissions.
Code:
# ============================== #
# Definition of variables #
# ============================== #
$UserName = "Domain\UserName"
$UserPassword = 'password'
$computer = 'ServerName'
$WebSiteName = 'Site-Build'
# ============================== #
# Generic setup #
# ============================== #
$SecurePassword = ConvertTo-SecureString -AsPlainText -Force -String $UserPassword
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName , $SecurePassword
# ========================== #
# Option 1 #
# ========================== #
$a = {
function foo([string]$WebSiteName){
Write-Host "Restarting website $WebSiteName"
Import-Module WebAdministration
Stop-WebSite $WebSiteName
Start-Sleep -Milliseconds 5000
Start-WebSite $WebSiteName
return "$WebSiteName"
}
foo($args)
}
$rv = Invoke-Command -Credential $Credential -ComputerName $computer -ScriptBlock $a -ArgumentList $WebSiteName
Write-Host $rv1
# ========================== #
# Option 2 - preffered by me #
# ========================== #
Start-Sleep -Milliseconds 15000
function foo([string] $WebSiteName){
Write-Host "Restarting website $WebSiteName"
Import-Module WebAdministration
Stop-WebSite $WebSiteName
Start-Sleep -Milliseconds 5000
Start-WebSite $WebSiteName
return "$WebSiteName"
}
$rv1 = Invoke-Command -Credential $Credential -ComputerName $computer -ScriptBlock ${function:foo} -ArgumentList $WebSiteName
Write-Host $rv1
<# #>
No comments:
Post a Comment