Reputation: 19
I am using a Powershell (.ps1) script for scanning the physical application and web servers to identify the configuration files related to our application which have hard coded IP addresses specified in them.
Powershell version -1.0 for the Windows server on which i am executing the script and getting error.
I am getting " You cannot call a method on a null valued expression "
The scenario is: running the script on my local physical machine works fine, however, running it on my application server gives me the above error.
$itemsFromFolder = Get-ChildItem -path $drive -include *.txt, *web.config, *.htm, *.html, *.asp, *.ascx, *.aspx, *.xml, *.js, *.css, *.sql -recurse -errorAction SilentlyContinue
Write-host -message "initializing object"
$itemsToScan = New-Object System.Collections.Generic.List[string]
Write-host -message "initializing object $itemsToScan
foreach($item in $itemsFromFolder)
{
$itemsToScan.Add($item.FullName)
#Write-host -message "itemstoscan loop $itemsToScan"
}
I am instantiating an object for System.Collections.Generic.List[string] which will be contained in variable $itemstoscan which will have items to scan for the IP pattern which I am providing.
Questions:
$itemsToScan = New-Object System.Collections.Generic.List[string]
is this the right way to instantiate object in powershell 1.0 which is restricting me running the same script on machines of different config?
$itemsToScan.Add($item.FullName)
I am getting error in evaluating this expression on my App server which is running fine on my local.
Upvotes: 1
Views: 267
Reputation: 18062
As I suspected you can achieve the desired result much easier:
$itemsFromFolder = Get-Childitem -path $drive -include *.txt, *web.config, *.htm, *.html, *.asp, *.ascx, *.aspx, *.xml, *.js, *.css, *.sql -recurse -errorAction SilentlyContinue
$itemsToScan = $itemsfromfolder | Select-Object FullName
With PowerShell you shouldn't bother extracting string values anymore but use the advantages of passing around actual objects.
Upvotes: 1
Reputation: 16626
There is no "out-of-the-box" support for creating generic types in Powershell v1 (read: you are using v2 syntax in v1).
$itemsToScan = New-Object System.Collections.Generic.List[string]
will throw an exception in Powershell v1 and the variable itemToScan
will be $null
. As a workaround you could resort to using New-GenericObject.
Upvotes: 3