预期行为:
- 有一个浏览文件目录并获取所有目录详细信息的 PS 脚本(工作)
- 将这些详细信息写回 SQL 表(工作)
该脚本正在运行,但失败并出现如下错误:
ForEach-Object:使用“0”参数调用“ExecuteNonQuery”的异常:“'EGIDIO' 附近的语法不正确”
我知道这是由撇号引起的。失败时读取的文件夹名为“D'EGIDIO, SHAH &TOWNSEND”。
问题是我似乎无法弄清楚如何在 PS 脚本中替换该文本,或者从脚本中插入 sql。无论我尝试什么它仍然失败。
#Invoke-sqlcmd Connection string parameters
$params = @{'server'='TESTServer';'Database'='_Maintenance'}
#Server to query WMI class win32_logicalDisks
$server = 'TESTServer'
#Prepare Insert Statement
$insert = @'
INSERT INTO [_PBS_Maintenance].[dbo].[Maint_DiskSpace](Path,SizeInMB,SizeInGB,FileLastModified,CreatedDate,CreatedBY)
VALUES (
'{0}',
'{1}',
'{2}',
'{3}',
GetDate(),
SYSTEM_USER
)
'@
write-host $insert
Try {
#Define connction string of target database
$connectionString = 'Data Source=PBSNV800IMG1\PBSNV800IMG1;Initial Catalog=_PBS_Maintenance;Integrated Security=SSPI'
# connection object initialization
$conn = New-Object System.Data.SqlClient.SqlConnection($connectionString)
#Open the Connection
$conn.Open()
# Prepare the SQL
$cmd = $conn.CreateCommand()
$array= @()
$folder = "\\fsvr\Public\Users\Valorie L\Valorie\ATTORNEY\EIC-ERMG\"
$Source = $folder
Get-ChildItem -Recurse $Source | Where-Object { $_.PSIsContainer } |
ForEach-Object {
$obj = New-Object PSObject
$SizeMB = [Math]::Round((Get-ChildItem -Recurse $_.FullName | Measure-Object Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB, 2)
$SizeGB = [Math]::Round((Get-ChildItem -Recurse $_.FullName | Measure-Object Length -Sum -ErrorAction SilentlyContinue).Sum / 1GB, 2)
$obj |Add-Member -MemberType NoteProperty -Name "Path" $_.FullName
$obj |Add-Member -MemberType NoteProperty -Name "SizeMB" $SizeMB
$obj |Add-Member -MemberType NoteProperty -Name "SizeGB" $SizeGB
$obj |Add-Member -MemberType NoteProperty -Name "DateModified" $_.LastWritetime
$array +=$obj
select Path,SizeMB,DateModified
ForEach-Object{
$cmd.CommandText = $insert -f $obj.Path,$obj.SizeMB,$obj.SizeGB, $obj.DateModified
$cmd.ExecuteNonQuery()
}
}
#Close the connection
$conn.Close()
}
Catch {
Throw $_
}
Invoke-Sqlcmd @params -Query "SELECT * FROM Maint_DiskSpace" | format-table -AutoSize