I'm using a tool called MobaXterm to open up SSH sessions to Linux Virtual Machines. I'm attempting to create an import file of hostnames from a script so I can dynamically create the list of VM's I want to connect to without adding them manually in the MobaXterm Gui. To that end I've created the following PowerShell script that reads in the Hostname and the IP address from a .csv file. The script is working in that an .mxtsessions file is being created and the file appears to be what is exported from my testing an export of sessions file from MobaXterm. Here is my working script:
$csvFilename = 'C:\mobaxterm\mobaXterm.csv'
$outfile = 'C:\mobaxterm\MobaXterm_Sessions.mxtsessions'
$csv = Import-Csv -Path $csvFilename -Delimiter ','
@'
[Bookmarks]
SubRep=
ImgNum=42
'@ | Out-File -FilePath $outfile
$output = foreach ($line in $csv) {
"$($line.hostname)= #109#0%$($line.ip)%22%[loginuser]%%-1%-1%%%22%%0%0%0%%%-1%0%0%0%%1080%%0%0%1#MobaFont%10%0%0%0%15%236,236,236%0,0,0%180,180,192%0%-1%0%%xterm%-1%0%0,0,0%54,54,54%255,96,96%255,128,128%96,255,96%128,255,128%255,255,54%255,255,128%96,96,255%128,128,255%255,54,255%255,128,255%54,255,255%128,255,255%236,236,236%255,255,255%80%24%0%1%-1%<none>%%0#0#"
}
$output | Out-File -FilePath $outfile -Append
The import file is simply a .csv file of two columns where column one has the hostname and column 2 has the IP address of each hostname.
As I said my script appears to be working in that it's creating a file that appears to be valid...but when I try to import this .mxtsessions file into MobaXterm it won't load. No errors are shown. Perhaps there's a log I can view for why the import fails?
to further triage this issue I then manually added some machines to my MobaXterm manually and exported the file. I've compared the exported file to the file I've created with my PowerShell script. I'm not seeing any differences between both files. The properties on both files look identical (except for the name of course). The data within each file are identical from my compare.
Can anyone provide some pointers for me on why my generated .mxtsessions file won't load into MobaXterm? I've looked in the MobaXterm.log file and I'm not seeing any errors related to my import? Has anyone else created an import sessions file and successfully imported it into MobaXterm?
Any advice or pointers this forum can provide me would be greatly appreciated.
Thank you.
From just testing it, I think it's a character encoding issue. MobaXTerm import works if I save as ASCII or UTF8-sans-BOM, doesn't work otherwise.
If you only have ASCII characters, try adding an encoding parameter when writing:
'@ | Out-File -FilePath $outfile -Encoding ASCII
$output | Out-File -FilePath $outfile -Append -Encoding ASCII
If you need Unicode, there's no way to write it without BOM from PowerShell 5.1 or earlier, so you'll need:
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines($outfile, $allyourtextcontent, $Utf8NoBomEncoding)