如何通过 SSIS 在管理员模式下执行 powershell 脚本

How to execute a powershell script in Admin mode via SSIS

我有一个 powershell 脚本,使用 PoshWSUS 从 WSUS 获取所有计算机。我在管理员模式下打开Powershell后手动执行脚本。

我现在必须使用 SSIS 执行脚本。我在控制流中插入了 Execute Process Task。可执行文件设置为 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

这是参数:-NoProfile -ExecutionPolicy ByPass -command ". c:\mypath\GetWSUSList.ps1" -verb runAs

我已经尝试了很多其他的,主要包括在这个页面中:PowerShell: Running a command as Administrator

但是其中 none 个有效并且仍然出现未授权错误。任何帮助将不胜感激。

我找到了这个 link Automate Running PowerShell Scripts that Require Admin elevation via SSIS。感觉跟你的问题差不多,可以参考一下。

解决方法如下:

步骤 1: 创建一个 powershell 脚本 文件。我的 script.ps1 是:

import-module poshwsus 
ForEach ($Server in Get-Content $WSUSServers)
{
    & connect-poshwsusserver $Server -port $WSUSPort | out-file $ProcessLog -append
    & Get-PoshWSUSUpdateSummaryPerClient -UpdateScope (new-poshwsusupdatescope) -ComputerScope (new-poshwsuscomputerscope) | Select Computer, LastUpdated | export-csv -NoTypeInformation -append $FileOutput
}

第 2 步: 创建一个 .bat 文件,假设它被称为 RunMyPS1.bat,如下图。

@ECHO OFF
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Scripts\WSUSReport\script.ps1""' -Verb RunAs}"
PAUSE

请注意,在参数行末尾使用 -verb runAs 在这里非常重要。

步骤 3:创建一个 Task Scheduler 到 运行 你的 .bat 文件,命名为 "RunMyBat"例如。

打开任务计划程序,单击右侧菜单上的"Create Task"。在 General 下,确保具有最高权限 的复选框 运行 被选中,这非常重要。然后导航到 Actions 部分,通过浏览到您的 .bat 文件添加新操作。

步骤 4. 运行 通过 SSIS 的任务调度程序

将 "Execute Process Task" 添加到您的控制流中。确保任务的可执行文件设置为:"C:\Windows\System32\schtasks.exe" 并且参数为:“/运行 /TN "RunMyBat",如下所示。

步骤 5. 运行 您的 SSIS 包。

重要:注意"execute process task"运行ning任务调度器被触发后,SSIS直接进入下一步(如果有的话)而不等待任务调度程序完成其过程。因此,如果有任何任务将使用您的 PowerShell 脚本的输出或更新数据,请插入一个 "Script Task" 并添加睡眠以确保您的 PowerShell 脚本已完成。

System.Threading.Thread.Sleep(120000);