'Script that doesn't validate argument

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://docs.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 = ""

}
} 


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ANT936