1

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:

  1. $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?

  2. $itemsToScan.Add($item.FullName) I am getting error in evaluating this expression on my App server which is running fine on my local.

3
  • 1
    How do you retrieve $item? It seems you are things more complicating than they are. Could you post a more of your script? Commented Jun 9, 2012 at 12:01
  • Please add more of your code - it seems like your problems result from the fact that you are trying to create a PowerShell script structured like a .Net program. Commented Jun 11, 2012 at 8:21
  • Filburt, As per your requirement below is the code snippet. $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" } Thanks Sumit N Commented Jun 13, 2012 at 14:47

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jon for the answer ... The problem is with acceptability of syntax in different version... I will try out with your workaround and will let you know if something is needed. Thanks Again....
1

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.

1 Comment

Thanks Filburt for your response .... I have used the below which is working as expected.. I made this change in the syntax of creating new object: $itemsToScan = New-Object System.Collections.ArrayList From the old way, which was: $itemsToScan = New-Object System.Collections.Generic.List[string] thats a good learning for me !!!! Thanks a lot for all the assistance ....

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.