简体   繁体   中英

Powershell Passing Variable to Function

I don't understand what is going on...

In the VerifyEmailSettings function, the $AdminEmailAddress is one of many parameters i can pass to the ps command i am using.

I want to be able to pass the paramater name, and value to other functions like below. However, when i pass this along, i get some odd results. As you can see in the results, trying to print the $SettingName in the VerifyEmailSettings function echos AdminEmailAddress admin@superuser.com Verified, Same instead of what i want... AdminEmailAddress Verified, Same The "admin@superuser.com is mixed in there somehow. Same happens with the $SetName in the SetEmailSettings functions.

Thanks in advance!!

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
}

Here is the results i get:

Starting Script
FSRM Installed, Skipping Install
Checking Email Settings
AdminEmailAddress admin@superuser.com Verified, Same
AdminEmailAddress admin@superuser.com

Do not call PowerShell functions with parentheses and commas

VerifyEmailSettings([string]"AdminEmailAddress",[string]$AdminEmailAddress)

What you're actually doing here is passing an array containing both values as the first argument and nothing for the second argument. That should be written like this:

VerifyEmailSettings "AdminEmailAddress" $AdminEmailAddress

Or

VerifyEmailSettings -SettingName "AdminEmailAddress" -SettingData $AdminEmailAddress

(there is no need to cast your strings as [string] )

Use Strict Mode

What you've done is a common error in PowerShell, made more common by the fact that you do use parentheses and commas when calling methods on .Net objects. I still do this once in a while after years of using PowerShell.

You can set strict mode which actually catches this for you and warns you about it:

Set-StrictMode -Version 2.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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