results of Out-File has extra characters

I’m trying to out put the results of a command to a cmd file but get characters added.

I take the results of mountvol and remove all the lines I don’t want, and modify the results to add text at the front and end of the lines. But it errors as there is a hidden character “more 0<\?”.

I’ve tried using the “-Encoding” switch but not had any success. Any suggestions ?

 

cmd is this

$cmd = $(Join-Path $env:TEMP listVormBind.txt)
Invoke-Command -ScriptBlock { mountvol > $cmd ; (Get-Content $cmd | Where-Object {$_ -match "Volume{" } ).Replace("\\?\","more < \\?\").Replace("}\","}:VormBind").trimstart() } |`
out-file $cmd.replace(".txt",".cmd") -Force

Example of what I get from mountvol

\\?\Volume{b137e4c8-2e49-4ef0-b482-f8f5eef92c36}\
C:\

\\?\Volume{1c5545c9-8c46-4723-a93f-5f43dcb6376b}\
T:\

\\?\Volume{5283dac6-2898-11e9-80c3-4cd98f040d85}\
Q:\

\\?\Volume{75952053-4133-49b1-b503-36c507a7cc96}\
D:\

\\?\Volume{b018b93c-d8c0-46ad-b49e-01ea5335874d}\
D:\MOUNT\Data\

\\?\Volume{0a71066f-81f4-4571-a637-f82624f0d9dd}\
D:\MOUNT\Log\

\\?\Volume{68d8b178-e315-405d-ae5d-91276add6783}\
D:\MOUNT\Backups\

\\?\Volume{247790dc-af4d-4083-b359-461c921c7918}\
G:\

\\?\Volume{bbb284c8-6c64-4a53-8450-1ddf818d687e}\
G:\MOUNT\Data\

\\?\Volume{cac98158-74c8-48e0-b7d3-b56e1a8050ec}\
G:\MOUNT\Log\

\\?\Volume{2a8330d8-5c1b-403c-b445-c892527d2ce4}\
G:\MOUNT\Backups\

Example of what I end up with in the cmd file:

more < \\?\Volume{b137e4c8-2e49-4ef0-b482-f8f5eef92c36}:VormBind
more < \\?\Volume{1c5545c9-8c46-4723-a93f-5f43dcb6376b}:VormBind
more < \\?\Volume{5283dac6-2898-11e9-80c3-4cd98f040d85}:VormBind
more < \\?\Volume{75952053-4133-49b1-b503-36c507a7cc96}:VormBind
more < \\?\Volume{b018b93c-d8c0-46ad-b49e-01ea5335874d}:VormBind
more < \\?\Volume{0a71066f-81f4-4571-a637-f82624f0d9dd}:VormBind
more < \\?\Volume{71390221-655f-4769-a615-45bcc9be4e96}:VormBind
more < \\?\Volume{df4265d7-a07b-4eb0-9bda-8eb732cc1c53}:VormBind
more < \\?\Volume{75bcfab8-9abe-488d-94ef-b26bc9e5d441}:VormBind

but when i run the resulting cmd file, i get an error:

C:\Windows\system32>more 0<\\?\Volume{b137e4c8-2e49-4ef0-b482-f8f5eef92c36}:VormBind
The system cannot find the file specified.

C:\Windows\system32>more 0<\\?\Volume{1c5545c9-8c46-4723-a93f-5f43dcb6376b}:VormBind
The system cannot find the file specified.

C:\Windows\system32>more 0<\\?\Volume{5283dac6-2898-11e9-80c3-4cd98f040d85}:VormBind
The system cannot find the file specified.

C:\Windows\system32>more 0<\\?\Volume{75952053-4133-49b1-b503-36c507a7cc96}:VormBind
The system cannot find the file specified.

C:\Windows\system32>more 0<\\?\Volume{b018b93c-d8c0-46ad-b49e-01ea5335874d}:VormBind
The system cannot find the file specified.

C:\Windows\system32>more 0<\\?\Volume{0a71066f-81f4-4571-a637-f82624f0d9dd}:VormBind
The system cannot find the file specified.

C:\Windows\system32>more 0<\\?\Volume{71390221-655f-4769-a615-45bcc9be4e96}:VormBind
The system cannot find the file specified.

C:\Windows\system32>more 0<\\?\Volume{df4265d7-a07b-4eb0-9bda-8eb732cc1c53}:VormBind
The system cannot find the file specified.

 

you can try without saving it to a file.

(mountvol | Where-Object {$_ -match "Volume{" } ).Replace("\\?\","more < \\?\").Replace("}\","}:VormBind").trimstart() 

I don’t know why you are using Invoke-Command here, you can execute it without using invoke command.

Because I’m going to be executing it on 4 servers remotely…just testing it locally right now.

btw the results are the same, using just the simple local command.

I get this:

more < \\?\Volume{b137e4c8-2e49-4ef0-b482-f8f5eef92c36}:VormBind
more < \\?\Volume{1c5545c9-8c46-4723-a93f-5f43dcb6376b}:VormBind
more < \\?\Volume{5283dac6-2898-11e9-80c3-4cd98f040d85}:VormBind
more < \\?\Volume{75952053-4133-49b1-b503-36c507a7cc96}:VormBind
more < \\?\Volume{b018b93c-d8c0-46ad-b49e-01ea5335874d}:VormBind
more < \\?\Volume{0a71066f-81f4-4571-a637-f82624f0d9dd}:VormBind
more < \\?\Volume{71390221-655f-4769-a615-45bcc9be4e96}:VormBind
more < \\?\Volume{df4265d7-a07b-4eb0-9bda-8eb732cc1c53}:VormBind
more < \\?\Volume{75bcfab8-9abe-488d-94ef-b26bc9e5d441}:VormBind
more < \\?\Volume{68d8b178-e315-405d-ae5d-91276add6783}:VormBind
more < \\?\Volume{7bed7244-3209-4569-a4f3-ba42bc976d53}:VormBind
more < \\?\Volume{247790dc-af4d-4083-b359-461c921c7918}:VormBind
more < \\?\Volume{bbb284c8-6c64-4a53-8450-1ddf818d687e}:VormBind
more < \\?\Volume{cac98158-74c8-48e0-b7d3-b56e1a8050ec}:VormBind
more < \\?\Volume{2a8330d8-5c1b-403c-b445-c892527d2ce4}:VormBind
more < \\?\Volume{66cc3ba5-9d4f-45ca-8eb1-787d22b72d3c}:VormBind
more < \\?\Volume{52e14aec-9d7f-4173-ad27-f59f157fb27c}:VormBind
more < \\?\Volume{6d305ccf-8a01-432b-964d-f1a404c47437}:VormBind
more < \\?\Volume{bac4c5e0-6386-4027-a762-d17ef0c90943}:VormBind
more < \\?\Volume{ec492462-2f39-497f-a80a-8b6edacf0f64}:VormBind
more < \\?\Volume{8586bade-298d-11e9-80c6-f8f21e3cff41}:VormBind
more < \\?\Volume{f94a5596-afe6-11e9-80d3-f8f21e3cff40}:VormBind

If I run one of those commands, i get

C:\Windows\system32>more < \\?\Volume{b018b93c-d8c0-46ad-b49e-01ea5335874d}:VormBind
The system cannot find the file specified.

If I output to a cmd file and run the resulting cmd file, i get this:

C:\Windows\system32>■m
'■m' is not recognized as an internal or external command,
operable program or batch file.

Problem solved. The instructions I’d been given by Vormetric were in correct and was missing a .

This

Replace("}\","}:VormBind")

Should be this

Replace("}\","}\:VormBind")

Final script that works:

$cmd = $(Join-Path $env:TEMP listVormBind.txt)
$Listvormbind = $cmd.replace(".txt",".cmd")
mountvol > $cmd ; (Get-Content $cmd | Where-Object {$_ -match "Volume{" } ).Replace("\\?\","more < \\?\").Replace("}\","}\:VormBind").trimstart() |`
out-file $Listvormbind -Force -Encoding utf8
$command = @'
cmd.exe /C $Listvormbind
'@
Invoke-Expression -Command:$command