I’m trying to check to see if a variable is 0 or maybe blank… and react accordingly. Here is my following code:<\/p>\n
\n```\nIf($ClientFSA -eq 0 -and $ClientFSA -like ''){\n$FSA = @{\n\"LME\" = \"HC2\";\n\"ME\" = \"FSA\";\n\"FSA\"=\"FSA\";\n\"DC\" = \"DCA\";\n\"DCA\" = \"DCA\";\n\"PKG\" = \"PK\";\n\"TRN\" = \"TRN\";\n\"AVIDIA\" = \"ABH\";\n\"UMB\" = \"UMB\"\n}\n}Else{$FSA = @{$ClientLME = \"HC2\"; $ClientFSA = \"FSA\"; $ClientDCA = \"DCA\"}}\n\n$FSAImport = Import-Csv $File\nForEach($row in $FSAImport){\n$row.FSA_DCA = $FSA[$row.FSA_DCA]\n}\n```\n\n<\/code><\/pre>\n
Advertisement
$clientFSA is populated from an access query earlier in the script. My problem is when the access cells in question are blank I get the error that:<\/p>\n
Cannot index into a null array.\n$row.FSA_DCA = $FSA[$row.FSA_DCA]\n<\/code><\/pre>","upvoteCount":3,"answerCount":7,"datePublished":"2017-08-18T12:42:13.000Z","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"},"acceptedAnswer":{"@type":"Answer","text":"If(!$ClientFSA) {\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2017-08-18T13:01:39.000Z","url":"https://community.spiceworks.com/t/if-variable-is-0-or-array-convert/600776/4","author":{"@type":"Person","name":"Phil-Adler","url":"https://community.spiceworks.com/u/Phil-Adler"}},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
I’m trying to check to see if a variable is 0 or maybe blank… and react accordingly. Here is my following code:<\/p>\n
\n```\nIf($ClientFSA -eq 0 -and $ClientFSA -like ''){\n$FSA = @{\n\"LME\" = \"HC2\";\n\"ME\" = \"FSA\";\n\"FSA\"=\"FSA\";\n\"DC\" = \"DCA\";\n\"DCA\" = \"DCA\";\n\"PKG\" = \"PK\";\n\"TRN\" = \"TRN\";\n\"AVIDIA\" = \"ABH\";\n\"UMB\" = \"UMB\"\n}\n}Else{$FSA = @{$ClientLME = \"HC2\"; $ClientFSA = \"FSA\"; $ClientDCA = \"DCA\"}}\n\n$FSAImport = Import-Csv $File\nForEach($row in $FSAImport){\n$row.FSA_DCA = $FSA[$row.FSA_DCA]\n}\n```\n\n<\/code><\/pre>\n$clientFSA is populated from an access query earlier in the script. My problem is when the access cells in question are blank I get the error that:<\/p>\n
Cannot index into a null array.\n$row.FSA_DCA = $FSA[$row.FSA_DCA]\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2017-08-18T12:42:13.000Z","url":"https://community.spiceworks.com/t/if-variable-is-0-or-array-convert/600776/1","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"}},{"@type":"Answer","text":"Have you tried checking if the value is null?<\/p>\n
If($ClientFSA -eq $Null)\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2017-08-18T12:45:43.000Z","url":"https://community.spiceworks.com/t/if-variable-is-0-or-array-convert/600776/2","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"Why not make use of the type casting and use:<\/p>\n
if ( -NOT $ClientFSA)<\/p>\n
Instead?<\/p>","upvoteCount":2,"datePublished":"2017-08-18T12:47:26.000Z","url":"https://community.spiceworks.com/t/if-variable-is-0-or-array-convert/600776/3","author":{"@type":"Person","name":"dreid007","url":"https://community.spiceworks.com/u/dreid007"}},{"@type":"Answer","text":"
wow… I know I overlook simple solutions… but this one is leader in the clubhouse so far.<\/p>\n
All three of your answers are correct, just slightly different syntax essentially.<\/p>","upvoteCount":0,"datePublished":"2017-08-18T13:04:19.000Z","url":"https://community.spiceworks.com/t/if-variable-is-0-or-array-convert/600776/5","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"}},{"@type":"Answer","text":"
This is another one of those things that you’ll learn to look for, always assume that your value can be $null, which you’d think would be like 0, but its not. Always check for $null
<\/p>","upvoteCount":0,"datePublished":"2017-08-18T13:35:27.000Z","url":"https://community.spiceworks.com/t/if-variable-is-0-or-array-convert/600776/6","author":{"@type":"Person","name":"matthart5","url":"https://community.spiceworks.com/u/matthart5"}},{"@type":"Answer","text":"