从C#应用程序运行时,不会导入Web管理模块

时间:2016-06-07 02:25:05

标签: c# powershell

在我的按钮单击事件上,运行以下脚本。但是当服务执行时,它不会在文本文件中显示任何内容,导入模块WebAdministration行显示错误消息:

找不到名称为“WebAdministration”的提供商

相同的脚本从powershell cmd运行正常

我的剧本:

        $a = (Get-Service | Where {$_.DisplayName -Like "Microsoft Exchange*"} | % {$_.name + ' :' + $_.status}) -join "`n"


        write-output $a >> C:\temp.txt


            import-module WebAdministration
        $webapps = Get-WebSite Powershell
        $list = @()
        foreach ($webapp in get-childitem IIS:\AppPools\)
        {
                $name = "IIS:\AppPools\" + $webapp.name
                $item = @{}

                $item.WebAppName = $webapp.name
                $item.Version = (Get-ItemProperty $name managedRuntimeVersion).Value
                $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
                $item.UserIdentityType = $webapp.processModel.identityType
                $item.Username = $webapp.processModel.userName
                $item.Password = $webapp.processModel.password

                $obj = New-Object PSObject -Property $item
                $list += $obj
        }

        $abc = ($list | %{$_.Name+":"+ $_.ApplicatonPool+":"+$_.Protocol+":"+$_.PhysicalPath}) -join "`n"
        write-output "Details For Default WebApplication: `n $abc" >> C:\temp.txt
        cd iis:\
        $path ='Sites\Default Web Site\' + $website
        $abcd = (Get-WebConfiguration -Filter "System.WebServer/Security/Authentication/*" -PSPath $path | where {$_.enabled -eq $True} | % {$_.SectionPath + ' :' + $_.Location}) -join "`n" -replace "/system.webServer/security/authentication/",""
        write-output " Enable Authentication Mode For Given Websites:`n $abcd" >> C:\temp.txt

2 个答案:

答案 0 :(得分:0)

尝试以管理员身份运行Powershell脚本,无需导入WebAdministration模块

答案 1 :(得分:0)

是的,右键单击图标->“以管理员身份运行”,以管理员身份运行powershell / Exchange管理控制台。