【发布时间】:2016-07-04 20:47:22
【问题描述】:
我试图找出原因,我的 Get-ChilddItem 变量在运行下面的脚本块时返回一个空的 $null 值,而在 ISE 中,如果我手动输入变量值,我能够得到一个输出我在Get-ChildItem 命令之前选择的 .sql 文件。下面是我正在编写的函数的开头:
Function test{
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True]
[string]$client
)
# Define variables
$Master = 'master'
$Slave = 'slave'
$SourcePath = "C:\somefolder\folder"
$Masterfolder = "C:\somemasterfolder"
$Slavefolder = "C:\someslavefolder"
# Create folders
$clientdir2 = New-Item -Path $Slavefolder -name "$client" -ItemType Directory
# Create folder on secondary server
$clientdir = New-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\$master\somemasterfolder" -name "$client" -ItemType Directory
# Getting all sql files and copy them to the destination folder
$gflz = Get-ChildItem $SourcePath\* -Include __07*, __08*, __10*
# Copy
Copy-Item $gflz $clientdir2
# Replace values in each sql files
$cpz = @(Get-ChildItem $clientdir2\* -Include *.sql)
if ($cpz -ne $null) {
Foreach ($flz in $cpz) {
(Get-Content $flz.FullName) | ForEach-Object { $_ -replace'clientID', "$client" -replace 'C:\LSpath', `
"$Masterfolder" -replace'C:\LSPath2', "$Slavefolder" } | Set-Content $flz.FullName}}
因此,替换操作无法进行,因为它没有循环。 我是否出于预期目的以错误的方式写了这个? 你能指出我正确的方向吗? 谢谢!
【问题讨论】:
标签: windows powershell powershell-3.0