Windows Vista Forums

PowerShell issue ('Cannot convert value "and" to type "System.Int32")

  1. #1



    Junior Member
    Join Date : Dec 2010
    Posts : 12
    XP Professional 64 bit
    Local Time: 03:13 AM

    PowerShell issue ('Cannot convert value "and" to type "System.Int32")

    Hello All,

    I started a few weeks ago on the PowerShell programing and I got my answer from here only. So am putting up the error that I am getting now to get some help again.

    Scenario : I wrote a script that checks a Folder on a server and counts the number of files in it.Then when it finds the resulting number to be more then a specific value(threshold) it sends up a mail to the required alias. To break it all along the portion to send a mail is generated through DBmail using another .PS1 file.

    In the PS1 file containing the main checking of number of files I am trying to pass 2 parameters :

    1. Folder path on the server
    2. the Threshold value of number of files

    Now when I created a job to run this powershell script as :

    & "C:\PowerShell\Power.PS1" \\XXXX\\c$\Documents and Settings\ABC\Local Settings\Temp 10


    IT fails with the error :

    A job step received an error at line 1 in a PowerShell script. The corresponding line is '& "C:\PowerShell\Power.ps1" \\XXXX\\c$\Documents and Settings\ABC\Local Settings\Temp 10'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot convert value "and" to type "System.Int32". Error: "Input string was not in a correct format." '. Process Exit Code -1. The step failed.


    I really can not move forward on this.

    Please help.

      My System SpecsSystem Spec

  2.   


  3. #2



    Junior Member
    Join Date : Dec 2010
    Posts : 12
    XP Professional 64 bit
    Local Time: 03:13 AM


      Thread Starter

    Re: PowerShell issue ('Cannot convert value "and" to type "System.Int32")

    Just to add for reference Below is the code for the Power.Ps1 file that I have used :


    param
    (

    [string] $directory_string,
    [int] $benchmark

    )

    $date=get-date
    [int]$directory_file_count = 0

    # check that the directory exists.
    $does_directory_exist = (Test-Path $directory_string)


    # if it does, then continue
    if ($does_directory_exist)
    {


    # Check for the number of files on the directory
    $directory_file_count = (get-childitem $directory_string -name -recurse).count

    # If number of files exceeds the limit, send mail to notify

    if($directory_file_count -gt $benchmark)
    {
    & "C:\Powershell\DB_mail.ps1"
    $subject= "XXXX"
    $body= "XXXX"

    }

    else

    {
    & "C:\Powershell\DB_mail.ps1"
    $subject= "XXXX"
    $body= "XXXX "
    }

    }
    else
    {
    & "C:\Powershell\DB_mail.ps1"
    $subject = "XXXX"
    $body = " XXXX"
    }

      My System SpecsSystem Spec

  4. #3



    Banned

    Join Date : Dec 2009
    Posts : 3,375
    Vista Home Premium x86 SP2
    Local Time: 10:43 PM
    uk european union

     

    Re: PowerShell issue ('Cannot convert value "and" to type "System.Int32")

    Hello!

    Me again! You have progressed! Well done!

    Code:
    & "C:\PowerShell\Power.PS1" \\XXXX\\c$\Documents and Settings\ABC\Local Settings\Temp 10
    The issue, as you know, is the & symbol. It was expecting a number, and not an & symbol. I cannot quite work out from this how you are starting this. Can you please show me how you are starting this script? It it from a .job in C:\Windows\Tasks?

    I don't really understand this & symbol here to be quite honest.

    Thanks!

    Richard

      My System SpecsSystem Spec

  5. #4



    Junior Member
    Join Date : Dec 2010
    Posts : 12
    XP Professional 64 bit
    Local Time: 03:13 AM


      Thread Starter

    Re: PowerShell issue ('Cannot convert value "and" to type "System.Int32")

    Hi Richard,

    I am really glad to see you.really

    I am actually doing this as a SQL Server Job with just one step in which I run the Power.Ps1 script and the script checks the number of files and then accordingly sends mail to the required alias.

    Running the file from the Location "C:\PowerShell\power.ps1" from the local server and then passing the "\\XXXX\c$\Documents and Settings\ABC\Local Settings\Temp" as the path parameter or location where the script needs to check the number of files and passing "10" as the threshold number of files to decide which alert or mail to be sent.

    To add I have taken the path mentioned on the server by connecting to it first and the Folder path mentioned is shared and the SQL server account running the services is the same account that has administration acccess on all servers.

    I have even tried the code as :

    & "C:\PowerShell\Power.PS1" $directory_string= "\\XXXX\c$\Documents and Settings\ABC\Local Settings\Temp" $benchmark= 10

    but it still gives the same error.

    Last edited by rahul1911; 28 Dec 2010 at 01:46 PM. Reason: incomplete
      My System SpecsSystem Spec

  6. #5



    Banned

    Join Date : Dec 2009
    Posts : 3,375
    Vista Home Premium x86 SP2
    Local Time: 10:43 PM
    uk european union

     

    Re: PowerShell issue ('Cannot convert value "and" to type "System.Int32")

    Hello!

    Please bear with me! I am not very familiar with Powershell or SQL. I am certainly not the expert you really need, but am the only one on this forum who may be able to do something - taken from other programming languages. I don't mind if you re-post this at a Powershell forum, but I will do my best.

    This is probably blindingly obvious to the knowledgable network admin you are and I am not, but the & is causing the errors. What purpose does it (try to) serve in the SQL initialisation of this PowerShell Script?

    Thanks!

    Richard

      My System SpecsSystem Spec

  7. #6



    Junior Member
    Join Date : Dec 2010
    Posts : 12
    XP Professional 64 bit
    Local Time: 03:13 AM


      Thread Starter

    Re: PowerShell issue ('Cannot convert value "and" to type "System.Int32")

    I really feel dum at time on this Blog, but I thought that I should share the details of resolution for this issue.

    There was nothing wrong with the code that I had written to decide the Subject and the Body of the mail but was a problem with the script that I was using to send out mails using the DB mail feature in SQL Server. The small mistake that I was making was that I mentioned the name of the server and the database as :

    abc.xyz

    which in fact should have been [abc].[xyz]

    This resolved the problem for me. Hope it helps others too.

    Thanks for the help Richard.

      My System SpecsSystem Spec


PowerShell issue ('Cannot convert value "and" to type "System.Int32")
Similar Threads
Thread Forum
error code 39 on all device type: "Modems" & "Ports (COM & LPT)" Vista hardware & devices
"Extensible Authentication Protocol" service gives "The system cannot find the file specified" error Vista networking & sharing
Vista not wotking with "My Computer" or "Control Panel", "Screen Saver" Vista General
Error: "Cannot convert "System.Object[]" to "System.Int32"." PowerShell
interesting "issue" or "bug" with psobjects when calling DOTNET framework stuff PowerShell