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

rahul1911

Member
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 Computer

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 Computer

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 Computer

System One

  • Manufacturer/Model
    Dell XPS 420
    CPU
    Intel Core 2 Quad Q9300 2.50GHz
    Motherboard
    Stock Dell 0TP406
    Memory
    4 gb (DDR2 800) 400MHz
    Graphics Card(s)
    ATI Radeon HD 3870 (512 MBytes)
    Sound Card
    Onboard
    Monitor(s) Displays
    1 x Dell 2007FP and 1 x (old) Sonic flat screen
    Screen Resolution
    1600 x 1200 and 1280 x 1204
    Hard Drives
    1 x 640Gb (SATA 300)
    Western Digital: WDC WD6400AAKS-75A7B0

    1 x 1Tb (SATA 600)
    Western Digital: Caviar Black, SATA 6GB/S, 64Mb cache, 8ms
    Western Digital: WDC WD1002FAEX-00Z3A0 ATA Device
    PSU
    Stock PSU - 375W
    Case
    Dell XPS 420
    Cooling
    Stock Fan
    Keyboard
    Dell Bluetooth
    Mouse
    Advent Optical ADE-WG01 (colour change light up)
    Internet Speed
    120 kb/s
    Other Info
    ASUS USB 3.0 5Gbps/SATA 6Gbps - PCI-Express Combo Controller Card (U3S6)
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:

My Computer

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 Computer

System One

  • Manufacturer/Model
    Dell XPS 420
    CPU
    Intel Core 2 Quad Q9300 2.50GHz
    Motherboard
    Stock Dell 0TP406
    Memory
    4 gb (DDR2 800) 400MHz
    Graphics Card(s)
    ATI Radeon HD 3870 (512 MBytes)
    Sound Card
    Onboard
    Monitor(s) Displays
    1 x Dell 2007FP and 1 x (old) Sonic flat screen
    Screen Resolution
    1600 x 1200 and 1280 x 1204
    Hard Drives
    1 x 640Gb (SATA 300)
    Western Digital: WDC WD6400AAKS-75A7B0

    1 x 1Tb (SATA 600)
    Western Digital: Caviar Black, SATA 6GB/S, 64Mb cache, 8ms
    Western Digital: WDC WD1002FAEX-00Z3A0 ATA Device
    PSU
    Stock PSU - 375W
    Case
    Dell XPS 420
    Cooling
    Stock Fan
    Keyboard
    Dell Bluetooth
    Mouse
    Advent Optical ADE-WG01 (colour change light up)
    Internet Speed
    120 kb/s
    Other Info
    ASUS USB 3.0 5Gbps/SATA 6Gbps - PCI-Express Combo Controller Card (U3S6)
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 Computer

Back
Top