OK, this is either weird or I’m blind/doing something stupid. I’m creating an array of filenames using Get-ChildItem, and the array is being created, I’ve written it to the console to make sure; however, when I try to pull a file from the list using ForEach, the filename is blank. Is something wrong here, or am I as dumb as I’m starting to feel? I’ve been chasing this for over two days now. I know it’s a bit long, but I’m posting the entire script because I’m using inter-dependent functions.<\/p>\n

Function Install-Downloads\n{\n  $script:MSIFiles = Get-ChildItem -Path $TargetDir -Name '*.msi' -File\n  \n  #Inserted for logging to make sure the array(s) are created.\n  Write-HostLog \"`n`rMSIFiles\"\n  $MSIFiles | Format-Table -AutoSize\n \n  $script:EXEFiles = Get-ChildItem -Path $TargetDir -Name '*.exe' -File\n  \n  Write-HostLog \"`n`r$EXEFiles\"\n  $ExeFiles | Format-Table -AutoSize\n\n#Start processing downloads.\n  Try\n  {\n    if ($MSIFiles = '')\n    {\n      Write-HostLog \"`n`rNo MSI files downloaded.  Skipping...`n`r\"\n    }\n    else\n    {\n      Write-HostLog \"`n`rProcessing MSI files...`n`r\"\n      foreach ($File in $MSIFiles) \n      {\n        $MSIArguments = @(\n          '/i',\n          \"$File\"\n          '/qn',\n          '/norestart'\n        )\n      \n#This is the first sign something's wrong.  $File is blank.\n        Write-HostLog \"`n`rMSI = $File\"\n        Write-HostLog \"`n`rProcessing $File ...\"\n        Start-Process -FilePath $env:systemroot\\system32\\msiexec.exe -ArgumentList $MSIArguments -PassThru -Verb runas -Wait\n      }\n    }\n  }\n  \n  Catch\n  {\n    Write-HostLog \"`n`rError processing file $File.  See $ErrorLogFullPath for details.`n`r\"\n    $Line = Write-Line\n    Write-ErrorLog \"`n`r$Line\" -LogFile $ErrorLogFullPath\n    $LineNumber = Get-CurrentLine\n    Write-ErrorLog $LineNumber -LogFile $ErrorLogFullPath\n    Write-ErrorLog \"Exception Type: $($_.Exception.GetType().FullName)\" -LogFile $ErrorLogFullPath\n    Write-ErrorLog \"Exception Message: $($_.Exception.Message)\" -LogFile $ErrorLogFullPath\n  }\n      \n  Finally\n  {\n    Write-HostLog \"`n`rMSI installation is now complete!`n`r\"\n  }      \n}\n\n<\/code><\/pre>\n

Advertisement

This is the transcript log file.<\/p>\n

**********************\nHost Application: C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\PowerShell_ISE.exe\nPSVersion: 5.1.14393.206\nPSEdition: Desktop\nPSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14393.206\nBuildVersion: 10.0.14393.206\nCLRVersion: 4.0.30319.42000\nWSManStackVersion: 3.0\nPSRemotingProtocolVersion: 2.3\nSerializationVersion: 1.1.0.1\n**********************\nTranscript started, output file is C:\\Logs\\SpecialLogs\\AR_Test9\\\\AR_Test9-Log_2017.09.26-14.15.30.log\n...\n\nMSIFiles\nExternalDiskCache_amd64_en-US.msi\nrequestRouter_amd64.msi\nrewrite_amd64_en-US.msi\nWebDeploy_amd64_cs-CZ.msi\nWebFarm2_x64.msi\nWebPlatformInstaller_amd64_en-US.msi\n\nProcessing MSI files...\n\nMSI =\n\nProcessing  ...\nPS C:\\WINDOWS\\system32> TerminatingError(Start-Process): \"Cannot validate argument on parameter 'ArgumentList'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.\"\n\nError processing file .  See C:\\Logs\\SpecialLogs\\ARR_Test11\\\\ARR_Test11-ErrorLog_2017.09.26-14.50.31.log for details.\n\nMSI installation is now complete!\n\nThere were errors encountered during script execution.  Please see C:\\Logs\\SpecialLogs\\ARR_Test11\\\\ARR_Test11-ErrorLog_2017.09.26-14.50.31.log for details.\n**********************\n\n<\/code><\/pre>\n

Advertisement

@martin9700<\/a> @thomaslee<\/a><\/p>","upvoteCount":2,"answerCount":22,"datePublished":"2017-09-26T16:28:47.000Z","author":{"@type":"Person","name":"s31064","url":"https://community.spiceworks.com/u/s31064"},"acceptedAnswer":{"@type":"Answer","text":"

Look at line 402 (by my count) in the Try block. You have equal sign (=) for a condition statement instead of -eq. The equal sign assigns a value s instead of comparing values.<\/p>\n

Try\n  {\n    if ($MSIFiles = '')\n    {\n      Write-HostLog \"`n`rNo MSI files downloaded.  Skipping...`n`r\"\n    }\n...\n<\/code><\/pre>\n

Instead, it should be something like this:<\/p>\n

Try\n  {\n    if ($MSIFiles.Count -eq 0)\n    {\n      Write-HostLog \"`n`rNo MSI files downloaded.  Skipping...`n`r\"\n    }\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2017-09-26T17:17:14.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/5","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},"suggestedAnswer":[{"@type":"Answer","text":"

OK, this is either weird or I’m blind/doing something stupid. I’m creating an array of filenames using Get-ChildItem, and the array is being created, I’ve written it to the console to make sure; however, when I try to pull a file from the list using ForEach, the filename is blank. Is something wrong here, or am I as dumb as I’m starting to feel? I’ve been chasing this for over two days now. I know it’s a bit long, but I’m posting the entire script because I’m using inter-dependent functions.<\/p>\n

Function Install-Downloads\n{\n  $script:MSIFiles = Get-ChildItem -Path $TargetDir -Name '*.msi' -File\n  \n  #Inserted for logging to make sure the array(s) are created.\n  Write-HostLog \"`n`rMSIFiles\"\n  $MSIFiles | Format-Table -AutoSize\n \n  $script:EXEFiles = Get-ChildItem -Path $TargetDir -Name '*.exe' -File\n  \n  Write-HostLog \"`n`r$EXEFiles\"\n  $ExeFiles | Format-Table -AutoSize\n\n#Start processing downloads.\n  Try\n  {\n    if ($MSIFiles = '')\n    {\n      Write-HostLog \"`n`rNo MSI files downloaded.  Skipping...`n`r\"\n    }\n    else\n    {\n      Write-HostLog \"`n`rProcessing MSI files...`n`r\"\n      foreach ($File in $MSIFiles) \n      {\n        $MSIArguments = @(\n          '/i',\n          \"$File\"\n          '/qn',\n          '/norestart'\n        )\n      \n#This is the first sign something's wrong.  $File is blank.\n        Write-HostLog \"`n`rMSI = $File\"\n        Write-HostLog \"`n`rProcessing $File ...\"\n        Start-Process -FilePath $env:systemroot\\system32\\msiexec.exe -ArgumentList $MSIArguments -PassThru -Verb runas -Wait\n      }\n    }\n  }\n  \n  Catch\n  {\n    Write-HostLog \"`n`rError processing file $File.  See $ErrorLogFullPath for details.`n`r\"\n    $Line = Write-Line\n    Write-ErrorLog \"`n`r$Line\" -LogFile $ErrorLogFullPath\n    $LineNumber = Get-CurrentLine\n    Write-ErrorLog $LineNumber -LogFile $ErrorLogFullPath\n    Write-ErrorLog \"Exception Type: $($_.Exception.GetType().FullName)\" -LogFile $ErrorLogFullPath\n    Write-ErrorLog \"Exception Message: $($_.Exception.Message)\" -LogFile $ErrorLogFullPath\n  }\n      \n  Finally\n  {\n    Write-HostLog \"`n`rMSI installation is now complete!`n`r\"\n  }      \n}\n\n<\/code><\/pre>\n

This is the transcript log file.<\/p>\n

**********************\nHost Application: C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\PowerShell_ISE.exe\nPSVersion: 5.1.14393.206\nPSEdition: Desktop\nPSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14393.206\nBuildVersion: 10.0.14393.206\nCLRVersion: 4.0.30319.42000\nWSManStackVersion: 3.0\nPSRemotingProtocolVersion: 2.3\nSerializationVersion: 1.1.0.1\n**********************\nTranscript started, output file is C:\\Logs\\SpecialLogs\\AR_Test9\\\\AR_Test9-Log_2017.09.26-14.15.30.log\n...\n\nMSIFiles\nExternalDiskCache_amd64_en-US.msi\nrequestRouter_amd64.msi\nrewrite_amd64_en-US.msi\nWebDeploy_amd64_cs-CZ.msi\nWebFarm2_x64.msi\nWebPlatformInstaller_amd64_en-US.msi\n\nProcessing MSI files...\n\nMSI =\n\nProcessing  ...\nPS C:\\WINDOWS\\system32> TerminatingError(Start-Process): \"Cannot validate argument on parameter 'ArgumentList'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.\"\n\nError processing file .  See C:\\Logs\\SpecialLogs\\ARR_Test11\\\\ARR_Test11-ErrorLog_2017.09.26-14.50.31.log for details.\n\nMSI installation is now complete!\n\nThere were errors encountered during script execution.  Please see C:\\Logs\\SpecialLogs\\ARR_Test11\\\\ARR_Test11-ErrorLog_2017.09.26-14.50.31.log for details.\n**********************\n\n<\/code><\/pre>\n

@martin9700<\/a> @thomaslee<\/a><\/p>","upvoteCount":2,"datePublished":"2017-09-26T16:28:48.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/1","author":{"@type":"Person","name":"s31064","url":"https://community.spiceworks.com/u/s31064"}},{"@type":"Answer","text":"

Can you maybe please just post the few lines that you think are causing the issue?
\nI mean you just posted about 500 lines of code…<\/p>","upvoteCount":1,"datePublished":"2017-09-26T16:33:40.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

Agreed. Please cut all that out and take a gander at: PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a><\/p>","upvoteCount":0,"datePublished":"2017-09-26T16:44:47.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/3","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},{"@type":"Answer","text":"

I guess that was a bit long. Seeing it online it looks a whole lot bigger than in my editor where I can compress regions.<\/p>\n

I think the problem is somewhere in this function. The $MSIFiles array is getting created, but I can’t pull a file out of it to process with the ForEach loop.<\/p>","upvoteCount":0,"datePublished":"2017-09-26T17:15:06.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/4","author":{"@type":"Person","name":"s31064","url":"https://community.spiceworks.com/u/s31064"}},{"@type":"Answer","text":"

Right but that parts , I think, will cause it to automatically go in the else loop.<\/p>\n

$MSIArguments = @(\n          '/i',\n          \"$File\"\n          '/qn',\n          '/norestart'\n        )\n\nStart-Process -FilePath $env:systemroot\\system32\\msiexec.exe -ArgumentList $MSIArguments -PassThru -Verb runas -Wait\n<\/code><\/pre>\n

And it says it can’t find the arguments?<\/p>","upvoteCount":0,"datePublished":"2017-09-26T17:24:23.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/6","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" Neally:<\/div>\n
\n

Right but that parts , I think, will cause it to automatically go in the else loop.<\/p>\n

\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\n

No. The line if ($MSIFiles = ‘’)<\/strong> will reset the value of $MSIFiles to a blank string. I tested it.<\/p>","upvoteCount":1,"datePublished":"2017-09-26T17:34:46.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/7","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"

Right, it says “Cannot validate argument on parameter ‘ArgumentList’. The argument is null or empty. …”. I know the array isn’t empty because I can dump it to the console and all of the filenames are there. It doesn’t seem to be able to pull $File from $MSIFiles.<\/p>","upvoteCount":0,"datePublished":"2017-09-26T17:38:31.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/8","author":{"@type":"Person","name":"s31064","url":"https://community.spiceworks.com/u/s31064"}},{"@type":"Answer","text":"

Yes, that’s for sure, I was jus wondering how if / else is evaluating it, if there is no real condition.<\/p>\n

It looks like it jumps in the if condition, rather than the else condition. interesting.<\/p>\n

if($test = 'somethingsilly'){\n    write-output 'IF'\n}else{\n    write-output 'ELSE'\n}\n\n# output is IF\n<\/code><\/pre>\n

I’d have thought it would have jumped to else as there isn’t a condition in the IF, but no condition seem to be a true. ¯_(ツ)_/¯<\/p>","upvoteCount":0,"datePublished":"2017-09-26T17:39:19.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/9","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

It’s weird if it does not jump in the else loop, why does it try the ‘start-process’?<\/p>","upvoteCount":0,"datePublished":"2017-09-26T17:41:06.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/10","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" Evan7191:<\/div>\n
\n
\n
\n<\/code><\/pre>\n<\/blockquote>\n

No. The line if ($MSIFiles = ‘’)<\/strong> will reset the value of $MSIFiles to a blank string. I tested it.<\/p>\n<\/blockquote>\n<\/aside>\n

Here is what happens if you use the equals sign (=) in a condition statement:<\/p>\n

PS> $MSIFiles = @(\"c:\\test1.msi\",\"c:\\test2.msi\")\n\nPS> $MSIFiles\nc:\\test1.msi\nc:\\test2.msi\n\nPS> If ($MSIFiles = '') {$True}\n\nPS> $MSIFiles\n\nPS> $MSIFiles.count\n1\n<\/code><\/pre>\n

The value of the variable gets reset rather than compared.<\/p>","upvoteCount":1,"datePublished":"2017-09-26T17:41:40.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/11","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" Neally:<\/div>\n
\n

It’s weird if it does not jump in the else loop, why does it try the ‘start-process’?<\/p>\n<\/blockquote>\n<\/aside>\n

My best guess is because the If statement did not evaluate to $True, so Powershell automatically went to Else, because that’s how Else is designed to work. I rarely use If . . . Else, because it’s too easy to overlook a possibility that could lead to Else unintentionally. I prefer ElseIf or Switch.<\/p>","upvoteCount":0,"datePublished":"2017-09-26T17:44:02.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/12","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" Evan7191:<\/div>\n
<\/blockquote>\n<\/aside>\n

Right, but the issue seem if, it jumps in the if loop, but according to the transcript log it is runing the else loop as it is trying to start the process?<\/p>\n

$test = @('stuff1','stuff2')\n\nif($test = ''){\n    write-output $test\n    # this is where the IF jumps to\n}else{\n    write-output $test\n    # but the error shows it's trying to run stuff here?\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2017-09-26T17:47:11.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/13","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
\n
\n
<\/div>\n\"\" s31064:<\/div>\n
\n

I know the array isn’t empty because I can dump it to the console and all of the filenames are there.<\/p>\n<\/blockquote>\n<\/aside>\n

Is that before or after the If ($MSIFiles = ‘’) statement?<\/p>","upvoteCount":0,"datePublished":"2017-09-26T17:48:48.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/14","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" Neally:<\/div>\n
\n
\n
\n
<\/div>\n\"\" Evan7191:<\/div>\n
<\/blockquote>\n<\/aside>\n

Right, but the issue seem if, it jumps in the if loop, but according to the transcript log it is runing the else loop as it is trying to start the process?<\/p>\n

$test = @('stuff1','stuff2')\n\nif($test = ''){\n    write-output $test\n    # this is where the IF jumps to\n}else{\n    write-output $test\n    # but the error shows it's trying to run stuff here?\n}\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\n

The If statement does not resolve to $True, because there is not a proper condition to evaluate. When the If statement does not resolve to $True, Powershell will go to Else, when it exists.<\/p>","upvoteCount":0,"datePublished":"2017-09-26T17:51:33.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/15","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"

I think I found it, you have no comma after $file<\/p>\n

$MSIArguments = @(\n          '/i',\n          \"$File\"\n          '/qn',\n          '/norestart'\n        )\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2017-09-26T17:54:23.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/16","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
\n
\n
<\/div>\n Evan7191:<\/div>\n
\n
<\/blockquote>\n

The If statement does not resolve to $True, because there is not a proper condition to evaluate. When the If statement does not resolve to $True, Powershell will go to Else, when it exists.<\/p>\n<\/blockquote>\n<\/aside>\n

Ya , I think that’s what I said earlier<\/p>\n

\n
\n
<\/div>\n \"\"\n Where Are My Filenames Going?<\/a> Programming & Development<\/span><\/span><\/a>\n <\/div>\n
\n Right but that parts , I think, will cause it to automatically go in the else loop. \n$MSIArguments = @(\n '/i',\n \"$File\"\n '/qn',\n '/norestart'\n )\n\nStart-Process -FilePath $env:systemroot\\system32\\msiexec.exe -ArgumentList $MSIArguments -PassThru -Verb runas -Wait\n\nAnd it says it can’t find the arguments?\n <\/blockquote>\n<\/aside>\n\n

I was just confused as when I tested it I gave it a random value, rather than leaving it blank \":confused:\"<\/p>","upvoteCount":0,"datePublished":"2017-09-26T17:56:23.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/17","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" Neally:<\/div>\n
\n

I think I found it, you have no comma after $file<\/p>\n

$MSIArguments = @(\n          '/i',\n          \"$File\"\n          '/qn',\n          '/norestart'\n        )\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\n

Good catch. That should fix the error message about a null argument.<\/p>","upvoteCount":1,"datePublished":"2017-09-26T17:57:14.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/18","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"

So technically we need 2 BAs, as without Evan’s finding the argument would always be blank, and without my finding the argument would always be invalid \":stuck_out_tongue:\"<\/p>","upvoteCount":0,"datePublished":"2017-09-26T17:59:58.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/19","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" Evan7191:<\/div>\n
\n

Look at line 402 (by my count) in the Try block. You have equal sign (=) for a condition statement instead of -eq. The equal sign assigns a value s instead of comparing values.<\/p>\n<\/blockquote>\n<\/aside>\n

AARRRGGGHHH!!! How did I not see that? I did the same thing last week in another script, but I found it in no time. It’s now able to pull the individual files. They’re not getting installed properly for some reason, but at least they’re being passed to the msiexec process.<\/p>\n

MSI = requestRouter_amd64.msi\n\nProcessing requestRouter_amd64.msi ...\n\nId      : 3676\nHandles : 22\nCPU     : 0.015625\nSI      : 2\nName    : msiexec\n\n... etc. \n\n<\/code><\/pre>\n

At least now I can troubleshoot something other than why it’s not passing filenames. Thanks for the help guys.<\/p>","upvoteCount":0,"datePublished":"2017-09-26T18:04:46.000Z","url":"https://community.spiceworks.com/t/where-are-my-filenames-going/608411/20","author":{"@type":"Person","name":"s31064","url":"https://community.spiceworks.com/u/s31064"}}]}}