不验证参数的脚本

发布于 2025-01-14 02:40:34 字数 4325 浏览 5 评论 0原文

我有这个脚本,它在 Word 文档中搜索“检测方法类型”,然后从第 5 行提取值,例如 “C:\program files\test\notepad.exe ",然后脚本应将其拆分为 $filepath = C:\program files\test$file = 记事本.exe。 尝试将上述内容传递给 new-cmdetectionclausefile 时出现以下错误:

New-CMDetectionClauseFile : Cannot validate argument on parameter 'FileName'. System.Management.Automation.ValidationMetadataException
At line:8 char:70
+ ...  new-CMdetectionClauseFile -path $filepathexe -filename $file -Existe ...
+                                                             ~~~~~
    + CategoryInfo          : InvalidData: (:) [New-CMDetectionClauseFile], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ConfigurationManagement.PowerShell.Cmdlets.Dcm.NewDetectionClauseFile*

错误: 即使 $file 已填充

先决条件,问题也会发生: 脚本 配置管理器.ps1 docx 包含:检测方法类型 C:\program files\test\notepad.exe 脚本:

param($documentPath)
function get-documentclause{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)][string]$documentPath

    )
$documentPath = "D:\Documents\Packaging Scripts\test.docx"
<#
    cells have an end-of-0cell marker of two chars (bytes 13 and 7)
    we will need to trim these later
    convert bytes to char and join as string
#>
$endOfCellMarker = -join @([char][byte]13, [char][byte]7)

# instantiate Word Com Object
$wordApplication = New-Object -ComObject Word.Application

<#
    open the document:
    param 1 = FileName
    param 2 = ConfirmConversions
    param 3 = ReadOnly$doclause

    (param 2 only needed because this method accepts only positional parameters)
    
    see https://learn.microsoft.com/en-us/office/vba/api/word.documents.open
#>
$wordDocument = $wordApplication.Documents.Open($documentPath, $false, $true)

$doclause = ""
$regclause = ""
$fileclause = ""
try{
#check to see if document is a table or paragraph
$count = 0
foreach($tables in $wordDocument.Tables){

$count++

}

#if document is true (paragraph) else it will be treated as table
if($count -eq 1){

$count = 0
foreach($para in $wordDocument.Paragraphs){
#Write-Host $para.range.text
#Write-Host "$count"
#$wordtolookfor = $para.Range(3).Text
$word = $para.Range.text
#$word
#pause
if ($word -like "*Detection Method Type*"){

$num = $count + 5

#Write-Host $wordDocument.Paragraphs($num).range.text
$doclause = $wordDocument.Paragraphs($num).range.text

} #end of if 

$count++

}#end of foreach



} else {$doclause = $wordDocument.Tables.Item(8).cell(4,2).range.text}
}catch{}


#test if the clause makes sense 

$doclause = $doclause.ToString().Trim()

#test if detection method contains exe or registry path
if(($doclause -like "*.exe*") -xor ($doclause -like "*CurrentVersion\Uninstall*")){

if($doclause -like "*.exe*") {


[string]$filepathexe = split-path -parent $doclause 

[string]$file = $doclause.Split("\") | Select-Object -last 1 


$fileclause = new-CMdetectionClauseFile -path $filepathexe -filename $file -Existence



} #end of exe clause
#if file does contain reg run the following 
elseif($doclause -like "*CurrentVersion\Uninstall*") {


#selects the first backslash
$hive = $doclause.Split("\") | Select-Object -First 1
#replaces the underscore withnothing
$hive = $hive.Replace("_","")
#removes the first 4 characters HKEY
$hive = $hive.substring(4)

#produce the rest of the key
$path = $doclause.Split("\") | Select-Object -last 7
$path = $path -join "\"
$path = Split-Path $path -Parent

#get final value
$regvalue = Split-Path -Leaf $doclause

$regclause = New-CMDetectionClauseRegistryKeyValue -Hive $hive -KeyName $path -PropertyType String -ValueName $regvalue -Existence


} #end of reg clause

else {
$regclause = ""
$fileclause = ""

}

} #end of testing clause

else {$doclause = ""}
$nothing = ""
# close the file and do not save changes
$wordDocument.Close([ref] [Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSaveChanges)

# end the WINWORD.EXE process
$wordApplication.Quit()

if($regclause){

return $regclause
#return @($hive,$path,$regvalue) this works but testing if previous would have worked too 

}
elseif($fileclause){return $fileclause}
else {
Return $nothing
$regclause = ""
$fileclause = ""

}
} 

I have this script, it searches a Word document for "Detection Method Type", then pulls the value from the 5th line along, say "C:\program files\test\notepad.exe", the script should then split it to $filepath = C:\program files\test and $file = notepad.exe.
I get the following error when trying to pass the above to new-cmdetectionclausefile:

New-CMDetectionClauseFile : Cannot validate argument on parameter 'FileName'. System.Management.Automation.ValidationMetadataException
At line:8 char:70
+ ...  new-CMdetectionClauseFile -path $filepathexe -filename $file -Existe ...
+                                                             ~~~~~
    + CategoryInfo          : InvalidData: (:) [New-CMDetectionClauseFile], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ConfigurationManagement.PowerShell.Cmdlets.Dcm.NewDetectionClauseFile*

ERROR:
the problem happens with the $file even though it's populated

pre-requisites:
myscript
configurationmanager.ps1
docx that contains: Detection Method Type C:\program files\test\notepad.exe
script:

param($documentPath)
function get-documentclause{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)][string]$documentPath

    )
$documentPath = "D:\Documents\Packaging Scripts\test.docx"
<#
    cells have an end-of-0cell marker of two chars (bytes 13 and 7)
    we will need to trim these later
    convert bytes to char and join as string
#>
$endOfCellMarker = -join @([char][byte]13, [char][byte]7)

# instantiate Word Com Object
$wordApplication = New-Object -ComObject Word.Application

<#
    open the document:
    param 1 = FileName
    param 2 = ConfirmConversions
    param 3 = ReadOnly$doclause

    (param 2 only needed because this method accepts only positional parameters)
    
    see https://learn.microsoft.com/en-us/office/vba/api/word.documents.open
#>
$wordDocument = $wordApplication.Documents.Open($documentPath, $false, $true)

$doclause = ""
$regclause = ""
$fileclause = ""
try{
#check to see if document is a table or paragraph
$count = 0
foreach($tables in $wordDocument.Tables){

$count++

}

#if document is true (paragraph) else it will be treated as table
if($count -eq 1){

$count = 0
foreach($para in $wordDocument.Paragraphs){
#Write-Host $para.range.text
#Write-Host "$count"
#$wordtolookfor = $para.Range(3).Text
$word = $para.Range.text
#$word
#pause
if ($word -like "*Detection Method Type*"){

$num = $count + 5

#Write-Host $wordDocument.Paragraphs($num).range.text
$doclause = $wordDocument.Paragraphs($num).range.text

} #end of if 

$count++

}#end of foreach



} else {$doclause = $wordDocument.Tables.Item(8).cell(4,2).range.text}
}catch{}


#test if the clause makes sense 

$doclause = $doclause.ToString().Trim()

#test if detection method contains exe or registry path
if(($doclause -like "*.exe*") -xor ($doclause -like "*CurrentVersion\Uninstall*")){

if($doclause -like "*.exe*") {


[string]$filepathexe = split-path -parent $doclause 

[string]$file = $doclause.Split("\") | Select-Object -last 1 


$fileclause = new-CMdetectionClauseFile -path $filepathexe -filename $file -Existence



} #end of exe clause
#if file does contain reg run the following 
elseif($doclause -like "*CurrentVersion\Uninstall*") {


#selects the first backslash
$hive = $doclause.Split("\") | Select-Object -First 1
#replaces the underscore withnothing
$hive = $hive.Replace("_","")
#removes the first 4 characters HKEY
$hive = $hive.substring(4)

#produce the rest of the key
$path = $doclause.Split("\") | Select-Object -last 7
$path = $path -join "\"
$path = Split-Path $path -Parent

#get final value
$regvalue = Split-Path -Leaf $doclause

$regclause = New-CMDetectionClauseRegistryKeyValue -Hive $hive -KeyName $path -PropertyType String -ValueName $regvalue -Existence


} #end of reg clause

else {
$regclause = ""
$fileclause = ""

}

} #end of testing clause

else {$doclause = ""}
$nothing = ""
# close the file and do not save changes
$wordDocument.Close([ref] [Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSaveChanges)

# end the WINWORD.EXE process
$wordApplication.Quit()

if($regclause){

return $regclause
#return @($hive,$path,$regvalue) this works but testing if previous would have worked too 

}
elseif($fileclause){return $fileclause}
else {
Return $nothing
$regclause = ""
$fileclause = ""

}
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过潦 2025-01-21 02:40:34

找到答案
之前

$fileclause = new-CMdetectionClauseFile -path $filepathexe -filename $file -Existence

添加:

$asd = [string]$file -replace"\u007",""
$asd2 = [string]$asd -replace"t|n|r",""`

然后更改
$fileclause = new-CMdetectionClauseFile -path $filepathexe -filename $asd2 -Existence

将 $file 导出到文本文件,您将得到预期结果加上“[]”符号,将其加载到记事本++中即可被解析为 0007,这是响铃符号的 Unicode,删除它以及其他的。

answer found
before

$fileclause = new-CMdetectionClauseFile -path $filepathexe -filename $file -Existence

add:

$asd = [string]$file -replace"\u007",""
$asd2 = [string]$asd -replace"t|n|r",""`

then change
$fileclause = new-CMdetectionClauseFile -path $filepathexe -filename $asd2 -Existence

exporting $file to a text file you get the expect result plus a "[]" symbol, loading that in to notepad++ it can be resolved to 0007 which is Unicode for a bell symbol, remove that and what not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文