2

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 [email protected] Verified, Same instead of what i want... AdminEmailAddress Verified, Same The "[email protected] is mixed in there somehow. Same happens with the $SetName in the SetEmailSettings functions.

Thanks in advance!!

Write-Host "Starting Script"
#Assigning Variables

$AdminEmailAddress = "[email protected]"
$SmtpServer = "exchange.local"
$FromEmailAddress = "[email protected]"


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 [email protected] Verified, Same
AdminEmailAddress [email protected]
3
  • 1
    You should not be using format-table, out-string, etc. to return data from a function. That converts everything to a string and you lose the benefit of objects. Commented Apr 2, 2015 at 21:47
  • @Bill_Stewart I took your advice and found i can use Get-FsrmSetting | Select -Expand $SettingName I like this much better and it cleaned by code. Thanks! Commented Apr 3, 2015 at 0:34
  • I hope someone can help me with one more thing. Every time i run this script i get an error. the whole 'the term GetInstallStatus is not recognized as the name...' I already have Set-ExecutionPolicy Unrestricted No idea what else to do. Commented Apr 3, 2015 at 1:33

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.