Skip to content

Latest commit

 

History

History
86 lines (72 loc) · 4.45 KB

File metadata and controls

86 lines (72 loc) · 4.45 KB

VBScript → PowerShell

한국어 | English

Microsoft deprecated VBScript in 2023. Windows 11 24H2 turned it into a Feature on Demand; a later phase is planned to ship it disabled by default, and removal follows after that.

SAP GUI's scripting engine is not affected — it is a COM interface and any COM capable language can drive it. What is going away is cscript.exe as a host, so the migration is a language port, not a redesign.

Script mapping

VBScript PowerShell
src/vbs/SapConnection.vbs src/ps/Connect-SapGui.ps1
src/vbs/SapTableInput.vbs src/ps/Set-SapTableColumn.ps1
src/vbs/ReservationExcelEdit.vbs src/ps/Edit-ReservationWorkbook.ps1
src/vbs/DateFormatCustom.vbs src/ps/ConvertTo-SapDate.ps1
src/vbs/BackupFolder.vbs src/ps/Backup-Folder.ps1
src/vbs/ExcelOpen.vbs one-liner, below
src/vbs/Deduplication.vbs one-liner, below
src/vbs/RegexCheck.vbs one-liner, below
src/vbs/FileLog.vbs one-liner, below

Four of the nine have no ported file on purpose. Writing a script whose body is a single built-in call adds a file to maintain and buys nothing.

# ExcelOpen.vbs
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.Workbooks.Open('C:\data\reservation.xlsx')

# Deduplication.vbs
('a,a,c,c,b' -split ',' | Select-Object -Unique) -join ','

# RegexCheck.vbs — StripPattern / MatchesPattern
'A-123-B' -replace '[^0-9]', ''
'A-123-B' -match '^\d+$'

# FileLog.vbs
Add-Content -Path C:\log\aae.log -Value 'started'
Get-Content -Path C:\log\aae.log -Raw     # ReadAllText
Get-Content -Path C:\log\aae.log          # ReadLines

Idiom translation

VBScript PowerShell Note
Option Explicit Set-StrictMode -Version Latest plus $ErrorActionPreference = 'Stop'
CreateObject("X") New-Object -ComObject X
GetObject(, "Excel.Application") [Runtime.InteropServices.Marshal]::GetActiveObject('Excel.Application') attaches to a running instance
GetObject("SAPGUI") [Microsoft.VisualBasic.Interaction]::GetObject('SAPGUI') needs Add-Type -AssemblyName Microsoft.VisualBasic; there is no ROT moniker equivalent in Marshal
WScript.Arguments.Item(0) param([Parameter(Mandatory)][string]$Name) named, validated, self documenting
WScript.Echo / .StdOut Write-Output Write-Host is for humans only
WScript.StdErr.WriteLine Write-Error / throw
WScript.Quit n exit n prefer throw and let the caller decide
On Error Resume Next try { } catch { } never blanket the whole file
Scripting.Dictionary @{} or [hashtable]
Scripting.FileSystemObject Get-Content / Set-Content / Copy-Item
Split(s, ",") s -split ',' regex by default; -split ',', 0, 'SimpleMatch' for literal
InStr(a, b) <> 0 $a.Contains($b) or -like
Replace(a, b, c) $a.Replace($b, $c) -replace is regex
Right("0" & x, 2) '{0:00}' -f $x
Password in argv [PSCredential] parameter see the security note in the README

Gotchas found while porting

  • GetActiveObject on PowerShell 7. [Runtime.InteropServices.Marshal]::GetActiveObject was dropped from .NET Core. Under PowerShell 7 either use the Windows PowerShell compatibility layer (Import-Module -UseWindowsPowerShell) or P/Invoke GetActiveObject from oleaut32.dll. Windows PowerShell 5.1 is the path of least resistance for the COM scripts here.
  • Release COM objects. [Runtime.InteropServices.Marshal]::ReleaseComObject in a finally, or Excel lingers as an invisible process holding the file open.
  • Encoding. Windows PowerShell 5.1 reads BOM-less files as ANSI, so a .ps1 containing Korean must be saved as UTF-8 with a BOM. PowerShell 7 defaults to UTF-8 and does not need it, but the BOM is harmless there.
  • Excel enum constants (xlValues = -4163, xlWhole = 1) are not defined when binding late. Declare them, as the scripts here do.
  • Approved verbs. Get-Verb lists them; Connect, Set, Edit, Backup and ConvertTo are all approved, which is why the file names look the way they do.