sql-server - Powershell参数路径的值为NULL

标签 sql-server powershell path patch service-pack

我已经基于服务器列表开发了一个PS1文件,该文件将负责应用SQL Server补丁。因此,它将读取一个文本文件,其中包含我需要修补和应用Patch的所有服务器。
我已经决定使用PARAM作为“源文件夹”(我将在其中获取服务器列表并记录输出); “目标文件夹”(我将可以在其中运行补丁程序),"file"(补丁程序的名称),“实例”(我将运行补丁程序更新的SQL Server实例)。
当我开始在下面运行命令时,它可以读取服务器列表(因此,第一个PARAM可以),但是,它在中止过程以下返回错误。
以下代码缺少什么或我在做什么错?

PS .:我也想使用Try ... Catch在输出文件上记录一条消息。我写的正确吗?
提前致谢!

[CmdletBinding()]
Param (
  [Parameter(Mandatory=$True,Position=0)]
  [string]$foldersource,

  [Parameter(Position=1)]
  [string]$folderdest,

  [Parameter(Position=2)]
  [string]$file,

  [Parameter(Position=3)]
  [string]$instance

)
foreach ($cluster in GC "$foldersource\Servers_List.txt")
{
    $output = "Server: $cluster Patch Installation on: $(Get-Date -format 'u')" 
try{
    Invoke-Command -ComputerName $cluster -ScriptBlock 
    {
        cd $folderdest
        .\$file /X:$folderdest
        Start-Sleep -s 10
        .\SETUP.exe /action=patch /instancename=$instance /quiet /IAcceptSQLServerLicenseTerms
    }
    -ErrorAction Stop; 
    $output += " SUCCESS"
   }
catch
   {
      $output += "Failed - $($_.exception.message)"
   }
$output | Out-File -Append $foldersource\Patch_Result_Non_SP.txt
} 

我如何在上面运行命令:。\ SQL_Server_install_non-Service_Pack_V2.ps1“D:\ Software \ Patch”“D:\ Software”“SQLServer2008R2-KB3045316-x64.exe”“MSSQLSERVER”
ERROR:

Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a non-null value.
+ CategoryInfo          : InvalidArgument: (:) [Set-Location],   PSArgumentNullException
+ FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SetLocationCommand
+ PSComputerName        : 

   The term '.\$file' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
+ CategoryInfo          : ObjectNotFound: (.\$file:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName        : 

The term '.\SETUP.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
+ CategoryInfo          : ObjectNotFound: (.\SETUP.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName        : 

最佳答案

您必须通过-ArgumentList$using约定将参数传递给Invoke-Command cmdlet。由于您未按照这种方式进行操作$folderdest,因此$fileInvoke-Command脚本块的范围内为null->脚本块定义了单独的范围!
Microsoft:

-ArgumentList

Supplies the values of local variables in the command. The variables in the command are replaced by these values before the command is run on the remote computer. Enter the values in a comma-separated list. Values are associated with variables in the order that they are listed. The alias for ArgumentList is Args.


还要通过Invoke-Command checkout Get-Help Invoke-Command -Examples cmdlet的示例。
如果您不喜欢ArgumentList解决方案,也可以使用remote variables
另外,您还应该定义Setup.exe的绝对路径!
因此您的代码应如下所示:
....
 Invoke-Command -ComputerName $cluster -ArgumentList $file, $folderdest, $instance -ScriptBlock 
{
    Param(
       [string] $rFile,
       [string] $rfileDest,
       [string] $rInstance
    )
    
    # Remove Write-Host after the script block works fine -> Write-Host is only a quick and dirty way to dump the variables content

    Write-Host $rFile
    Write-Host $rfileDest
    Write-Host $rInstance

    cd $rfileDest

    $someArgs = "/X:{0}" -f $rfileDest
    Start-Process -FilePath  $rFile -ArgumentList $someArgs -Wait -PassThru

    Start-Sleep -s 10

    $setupArgs = "action=patch /instancename={0} /quiet /IAcceptSQLServerLicenseTerms" -f $rInstance

    Start-Process -FilePath ".\Setup.exe" -ArgumentList $setupArgs -Wait -PassThru

}
....
希望能有所帮助。

关于sql-server - Powershell参数路径的值为NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46923735/

相关文章:

asp.net - 存储过程的 EF Core 自定义结果

powershell - 在不使用Microsoft Office的情况下阅读Word文档内容

android - 您应该如何使用带有 PathShape 的 ShapeDrawable 在自定义 View 上绘制线条?

c++ - 搜索.exe的路径

ANDROID_HOME 路径设置但未被读取

MySQL 将表 1 中的多个列连接到表 2 上的多个列

sql-server - 连接名字和姓氏作为名称,然后在 SSIS 中省略名字和姓氏

sql - 在 SQL SELECT 语句中重用别名字段

performance - Powershell 远程处理性能

xml - 用于创建本地用户并将详细信息写入 XML 文件的 Powershell 脚本