无法使用 powershell 通过 sql 数据库记录路径检查共享空间

无法使用 powershell 通过 sql 数据库记录路径检查共享空间

谁能告诉我为什么这些代码失败了?

1,我连接到数据库 2,我从数据库查询有用的信息(获取路径) 3,我检查了查询路径下的可用空间

这是我的代码:

######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()

我确信我查询的路径是有效的,因为我使用 cmd 测试了此路径。但我在这里失败了……错误日志是:

  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

答案1

发生第一个错误:

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

无法访问网络位置

那么 的值是多少$var.droppath?它可以在该脚本之外访问吗?

相关内容