我无法解决问题:
$myBuildTestNumber = "build-"+$env:BUILD_NUMBER;
$rootDirectory = "\\\\myRootPath\\";
$sn = "ibex.insight.reportrunner";
Import-Module E:\\PSSDeployService.ps1 -ArgumentList $myBuildTestNumber,$rootDirectory,$sn
Param(
[Parameter(Position=0,Mandatory=$false)][string]$BuildNumber,
[Parameter(Position=1,Mandatory=$false)][string]$rootDirectorys,
[Parameter(Position=2,Mandatory=$false)][string]$serviceName
)
Import-Module E:\PSSDeployServiceModule.psm1
# Here I am calling a function 'DeployServiceProc' from the .psm1 file with the parameters
DeployServiceProc $rootDirectorys $BuildNumber $serviceName;
function DeployServiceProc($rootDirectory, $BuildNumber, $serviceName) {
function backupServiceFiles($rd,$bn,$sn) {
"In Function 'backupServiceFiles'..."
"rd: ${rd}";
"Build Number: " + $bn;
"Service Name: " + $sn;
$serviceStatus = Get-Service $sn;
"Service Status: " + $serviceStatus;
if ($serviceStatus -eq 'Stopped') {
#"Service is stopped, taking backup";
}
}
Invoke-Command -ComputerName "myRemoteSystem.Name" -ScriptBlock ${function:backupServiceFiles} -ArgumentList $rootDirectory,$BuildNumber,$serviceName -Credential $Cred
}
问题是:如果我不提供三个参数,代码:
$serviceStatus = Get-Service $sn;
工作得很好。
但是,如果我提供所有三个参数,代码将返回:
服务状态:System.ServiceProcess.ServiceController
我无法确定我做错了什么。
您是否可以通过删除$ {rd}变量周围的引号来尝试运行命令并重试(可能这是使用时获得此输出的3个变量之一?):
"rd: ${rd}";
"rd:" ${rd};
对于某些类型的变量,使用引号输出变量类型,而不是变量的实际内容。
编辑1:
写的时候好的
$serviceStatus = Get-Service $sn;
如果我没有弄错的话,你会得到整个对象,但是你想显示状态?
所以尝试下一行代码
"Service Status: " + $serviceStatus.status;