我不明白发生了什么......
在VerifyEmailSettings
函数中,$AdminEmailAddress
是我可以传递给我正在使用的ps命令的众多参数之一。
我希望能够将参数名称和值传递给下面的其他函数。然而,当我通过这个时,我得到一些奇怪的结果。正如您在结果中看到的那样,尝试在$SettingName
函数echos VerifyEmailSettings
中打印AdminEmailAddress admin@superuser.com Verified, Same
而不是我想要的内容... AdminEmailAddress Verified, Same
" admin @ superuser.com以某种方式混合在一起。 $SetName
函数中的SetEmailSettings
也是如此。
提前致谢!!
Write-Host "Starting Script"
#Assigning Variables
$AdminEmailAddress = "admin@superuser.com"
$SmtpServer = "exchange.local"
$FromEmailAddress = "fsrm@omg.com"
If (GetInstallStatus){
Write-Host "FSRM Installed, Skipping Install"
Write-Host "Checking Email Settings"
VerifyEmailSettings([string]"AdminEmailAddress",[string]$AdminEmailAddress)
} else {
Write-Host "FSRM Not Installed, Installing"
Install-WindowsFeature –Name FS-Resource-Manager –IncludeManagementTools
If (GetInstallStatus){
Write-Host "FSRM Installed"
} else {
Write-Host "FSRM Error on Install, Halting"
#halt here
}
}
function GetInstallStatus {
$status = (Get-WindowsFeature -Name FS-Resource-Manager | ft Installed -autosize -hidetableheaders | out-string).trim();
return $status
}
function VerifyEmailSettings([string]$SettingName, [string]$SettingData) {
$Verify = (Get-FsrmSetting | Select-Object $SettingName | FT -autosize -hidetableheaders | Out-String).Trim()
If ($Verify -eq $SettingData) {
Write-Host $SettingName "Verified, Same"
SetEmailSettings([string]$SettingName, [string]$SettingData)
} Else {
Write-Host $SettingName "Wrong, Updating"
SetEmailSettings([string]$SettingName, [string]$SettingData)
}
}
function SetEmailSettings([string]$SetName, [string]$SetData) {
$SetName
#Set-FsrmSetting $SetName $SetData
}
以下是我得到的结果:
Starting Script
FSRM Installed, Skipping Install
Checking Email Settings
AdminEmailAddress admin@superuser.com Verified, Same
AdminEmailAddress admin@superuser.com
答案 0 :(得分:3)
VerifyEmailSettings([string]"AdminEmailAddress",[string]$AdminEmailAddress)
你在这里实际做的是传递一个数组,其中包含两个值作为第一个参数,而没有用于第二个参数。那应该是这样写的:
VerifyEmailSettings "AdminEmailAddress" $AdminEmailAddress
或者
VerifyEmailSettings -SettingName "AdminEmailAddress" -SettingData $AdminEmailAddress
(无需将字符串转换为[string]
)
您所做的是PowerShell中的一个常见错误,因为您在.Net对象上调用方法时执行使用括号和逗号这一事实更为常见。经过多年使用PowerShell,我仍然偶尔会这样做。
您可以set strict mode实际捕捉到这一点并向您发出警告:
Set-StrictMode -Version 2.0