繁体   English   中英

如何在具有域用户凭据的本地计算机上运行Powershell脚本

[英]How to run Powershell script on local computer but with credentials of a domain user

我必须实施一个解决方案,其中必须将SSIS项目(xy.ispac)从一台计算机部署到另一台计算机。 到目前为止,我已经设法从互联网的各个位置复制,粘贴以下内容:

# Variables
$ServerName = "target"
$SSISCatalog = "SSISDB" # sort of constant
$CatalogPwd = "catalog_password"

$ProjectFilePath = "D:\Projects_to_depoly\Project_1.ispac"

$ProjectName = "Project_name"
$FolderName = "Data_collector"

# Load the IntegrationServices Assembly
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices")

# Store the IntegrationServices Assembly namespace to avoid typing it every time
$ISNamespace = "Microsoft.SqlServer.Management.IntegrationServices"

Write-Host "Connecting to server ..."

# Create a connection to the server
$sqlConnectionString = "Data Source=$ServerName;Initial Catalog=master;Integrated Security=SSPI;"
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString

$integrationServices = New-Object "$ISNamespace.IntegrationServices" $sqlConnection

$catalog = $integrationServices.Catalogs[$SSISCatalog]

# Create the Integration Services object if it does not exist
if (!$catalog) {
    # Provision a new SSIS Catalog
    Write-Host "Creating SSIS Catalog ..."
    $catalog = New-Object "$ISNamespace.Catalog" ($integrationServices, $SSISCatalog, $CatalogPwd)
    $catalog.Create()
}

$folder = $catalog.Folders[$FolderName]

if (!$folder)
{
    #Create a folder in SSISDB
    Write-Host "Creating Folder ..."
    $folder = New-Object "$ISNamespace.CatalogFolder" ($catalog, $FolderName, $FolderName)            
    $folder.Create()  
}

# Read the project file, and deploy it to the folder
Write-Host "Deploying Project ..."
[byte[]] $projectFile = [System.IO.File]::ReadAllBytes($ProjectFilePath)
$folder.DeployProject($ProjectName, $projectFile)

这在开发机器-测试服务器对上似乎运行得异常好。 但是,实际环境会有所不同,正在执行部署工作的计算机(部署服务器或现在的DS)与要部署项目的SQL Server(简称DB)位于不同的域中,并且由于SSIS需要Windows身份验证,我将需要在DS上本地运行上述代码,但要在DB上使用用户的凭据。

这就是我失败的地方。 唯一有效的方法是使用runas /netonly /user:thatdomain\\anuserthere powershell启动Powershell命令行界面,输入密码,然后将脚本原封不动地粘贴到其中。 las,这不是一个选择,因为无法将密码传递给runas(使用/ savecred至少一次),并且用户交互也无法实现(整个过程必须自动化)。

我尝试了以下方法:

  • 只需在DS上运行脚本,行$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString将使用DB无法识别的DS中的凭据,并且New-Object没有-Credential arg可以传递给
  • 将所有内容放入带有-Credential的Invoke-Command中,也需要使用-Co​​mputername。 我想可以将本地用作“远程”(使用。作为Computername),但是它仍然抱怨访问被拒绝。 我正在扫描about_Remote_Troubleshooting,到目前为止没有任何成功。

关于如何克服此问题的任何提示?

一种解决方案可能是使用sql用户(具有正确的访问权限)代替使用的AD。 喜欢的东西, 应该工作。 (还检查答案以更正连接字符串)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM