Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Could anybody tell me why I failed on these code?

1, I connect to database 2, I query useful information from database (get path) 3, I checked the free space under queried path

Here are my codes:

######connect to windows authentication local database################################

$connection= new-object system.data.sqlclient.sqlconnection #Set new object to connect to sql database

$Connection.ConnectionString ="server=.;database=sharespace;trusted_connection=True" # Connectiongstring setting for local machine database with window authentication

Write-host "connection information:"

$connection #List connection information

Write-host "connect to database successful."

$connection.open() #Connecting successful

#########query drop paths############################################################

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand #setting object to use sql commands

$SqlQuery = "select Droppath from sharespace" #setting query "get drop paths"

$SqlCmd.CommandText = $SqlQuery # get query

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter #

$SqlAdapter.SelectCommand = $SqlCmd #

$SqlCmd.Connection = $Connection

$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet) 

#$Connection.Close()

$DataSet.Tables[0].rows[1]
#############check free space form drop paths###########################################


$var=$dataset.tables[0].rows[1]  #
#$var.system.data.datarow

$var.droppath

$map = new-Object -com wscript.Network 

$map.mapNetworkDrive("v:",$($var.droppath))

$fso = new-Object -com Scripting.FileSystemObject

$do = $fso.getdrive("v:")

$do.AvailableSpace

$do.TotalSize

#$var|get-member

$map.RemoveNetworkDrive("v:")

$Connection.Close()

I am sure the path I queried is valid cause I test this path by using cmd. But I failed here.... error logs are :

  Exception calling "MapNetworkDrive" with "2" argument(s): "The network location     cannot be reached. For information about network troubleshooting, see Windows Help."
  At D:\checkfreespace.ps1:47 char:21
  + $map.mapNetworkDrive <<<< ("v:",$($var.droppath))
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : ComMethodTargetInvocation

  Exception calling "GetDrive" with "1" argument(s): "Exception from HRESULT: 0x800A0044 (CTL_E_DEVICEUNAVAILABLE)"
  At D:\checkfreespace.ps1:51 char:20
  + $do = $fso.getdrive <<<< ("v:")
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : ComMethodTargetInvocation

  Exception calling "RemoveNetworkDrive" with "1" argument(s): "This network connection does not exist."
  At D:\checkfreespace.ps1:59 char:24
  + $map.RemoveNetworkDrive <<<< ("v:")
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : ComMethodTargetInvocation
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Your first error occurs:

$map.mapNetworkDrive <<<< ("v:",$($var.droppath))

The network location cannot be reached

So what it the value of $var.droppath, and is it accessible outside this script?

share|improve this answer
    
I tried $var.droppath at the very begainning, this failed either. I think the path is ok since I have test it via cmd, it is accessible. –  pansal Jul 8 '11 at 7:59
    
@Pansal: Try just that part of the script (mapping the drive) with a hardcoded path in PSH: does it work? –  Richard Jul 8 '11 at 8:01
    
:I tried that part with the path like this:$map.mapNetworkDrive("v:", "\\network\dirves\test1") . It works. –  pansal Jul 8 '11 at 9:37
    
@Pansal: I can be very confident then that either this is a permissions problem (is the script scheduled?). Or (and more likely): you are not passing the string you think you are to mapNetworkDrive. Have you tried using ISE's debugger to break at that line and examine precisely what is being passed? (General principle of debugging: if you know what you are passing and therefore is must work then you are wrong in both respects. Debugging is about overcoming your own assumptions.) –  Richard Jul 8 '11 at 10:25
2  
@Pansal: To remove spaces in PSH uses the TrimEnd method: $path = $var.droppath.TrimEnd() and then pass $path to mapNetworkDrive. –  Richard Jul 10 '11 at 6:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.