2023-08-30

PowerShell Script to Change Public IP Address of VM on Azure

需搭配 Connect-AzAccount with Saved Encryped Credential in Powershell 實現自動化


Param (
    [String]$VMName = ''
);

if ($VMName -eq '') {
    exit;
};

Set-Location -Path ('C:\ChangeVMPublicIP');

if (Test-Path -Path (($VMName) + '.Lock')) {
    if ((Get-Date) -lt (Get-Item -Path (($VMName) + '.Lock')).LastWriteTime.AddMinutes(5)) {
        write-Host 'VM Locked';
        exit;
    };
};
$VMName | Out-File (($VMName) + '.Lock');

$CredentailFileName = 'AzCredential.txt';
$AzSubscriptionID = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';

2023-08-28

PowerShell 取得 UTF-8 中英文夾雜之字串長度

  $StringLength = 0;
foreach ($Word in ($String -Split '')) {
if ([System.Text.Encoding]::UTF8.GetByteCount($Word) -gt 1) {
$StringLength += 2;
} else {
$StringLength += [System.Text.Encoding]::UTF8.GetByteCount($Word);
};
};