我正在试图找出原因,我的Get-ChilddItem
变量在运行下面的scriptblock时返回一个空的$ 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}}
因此,不能进行替换操作,因为它不会循环。 我是否以错误的方式为预期目的写这个? 你能指出我正确的方向吗? 谢谢!
答案 0 :(得分:0)
从Stackoverflow帖子PowerShell Script to Find and Replace for all Files with a Specific Extension中找出我的问题。
我非常确定Get-Content $sqlfiles | gm -MemberType Properties
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
PSChildName NoteProperty System.String
PSChildName=Mysqlfiles.sql
PSDrive NoteProperty System.Management.Automation.PSDriveInfo PSDrive=C
PSParentPath NoteProperty System.String
PSParentPath=C:\parentpath
PSPath NoteProperty System.String
PSPath=mysqlfilefullpathname.sql
PSProvider NoteProperty System.Management.Automation.ProviderInfo
PSProvider=Microsoft.PowerShell.Core\FileSystem
ReadCount NoteProperty System.Int64 ReadCount=1
Length Property int Length {get;}
命令中的files属性包含 Fullname 或 Name 作为成员。事实证明,当我检查成员时,我无法获得任何属性匹配FullName或Name:
user defined data formats
所以与FullName属性相关的唯一属性是 PSPath ,正如我在上面引用的帖子中所建议的那样。 定义正确的属性最终有助于替换每个文件中的字符串,就像我期望的那样。 致Daniel Liuzzi和Robben_Ford_Fan_boy的提示。