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';

##### Connect to Azure
[string[]]$CredentialRead = Get-Content -Path $CredentailFileName;
$UserName = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($CredentialRead[0]));
$Password = $CredentialRead[1] | ConvertTo-SecureString -Key (1..16)
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password;
$ConnectAzAccount = Connect-AzAccount -Credential $Credential -Subscription $AzSubscriptionID;
##### Connect to Azure

$ScriptStartTime = (Get-Date).ToUniversalTime().AddHours(8).ToString('yyyy-MM-dd HH:mm:ss');

$DateTimeString = (Get-Date).ToUniversalTime().AddHours(8).ToString('yyyyMMdd HHmmss')
$TranscriptLog = (Get-Item -Path ".\" -Verbose).FullName + "\Logs\" + ("ChangeVMPublic " + $DateTimeString + ".log")
$StartLog = start-transcript -path $TranscriptLog;

$VM = Get-AzVM -Name $VMName;

if ($VM -eq $Null) {
    exit;
};

$NIC = Get-AzNetworkInterface -ResourceId $VM.NetworkProfile.NetworkInterfaces.Id;
$Subnet = Get-AzVirtualNetworkSubnetConfig -ResourceId $NIC.IpConfigurations.Subnet.Id;
$PublicIP = Get-AzPublicIpAddress | where {$_.Id -eq $NIC.IpConfigurations.PublicIpAddress.Id};

if ($PublicIP -eq $Null) {
    exit;
};

$OldPublicIPAddress = $PublicIP.IpAddress;
$NewPublicIPAddress = $PublicIP.IpAddress;

while ($NewPublicIPAddress -eq $OldPublicIPAddress) {
    $NIC.IpConfigurations.publicipaddress.id = $Null;
    $SetNIC = Set-AzNetworkInterface -NetworkInterface $NIC;

    $SetNIC = $NIC | Set-AzNetworkInterfaceIpConfig -Name $NIC.IpConfigurations.Name -PublicIPAddress $PublicIP -Subnet $Subnet;
    $SetNIC = $NIC | Set-AzNetworkInterface;

    $PublicIP = Get-AzPublicIpAddress | where {$_.Id -eq $NIC.IpConfigurations.PublicIpAddress.Id};
    $NewPublicIPAddress = $PublicIP.IpAddress;
    write-host ('Public IP Address of ' + $VMName + ' has been changed from ' + $OldPublicIPAddress + ' to ' + $NewPublicIPAddress);
};

Write-Host ('Script execution time: ' + [math]::Round((New-TimeSpan -Start $ScriptStartTime -End (Get-Date).ToUniversalTime().AddHours(8).ToString('yyyy-MM-dd HH:mm:ss')).TotalSeconds) + ' Seconds');

$EndLog = stop-transcript;

if (Test-Path -Path (($VMName) + '.Lock')) {
    Remove-Item -Path (($VMName) + '.Lock') -Confirm:$False;
    exit;
};

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);
};
};

2023-02-13

2013 大阪地點

        日本大阪府近鉄日本橋駅

                https://maps.google.com.tw/maps?q=日本大阪府近鉄日本橋駅

 

        日本大阪府近鉄日本橋駅 10 號出口出來到黑門市場

                        https://maps.google.com.tw/maps?q=日本大阪府大阪市中央区日本橋2丁目4黑門市場

                        https://maps.google.com.tw/maps?q=34.66632,135.50628

 

        老爺爺起司蛋糕店

 

                買起司蛋糕店 & 布丁

 

                        https://maps.google.com.tw/maps?q=34.665238,135.501903

                        https://maps.google.com.tw/maps?q=日本大阪府大阪市中央区難波32りくろーおじさんの店ナンバ本店

 

        日本大阪府法善寺

 

                法善寺」可以參拜「不動明王」,順序是

 

2013 京都地點

---    京都 20 分鐘

 

        二条城前(バス)

                https://maps.google.com.tw/maps?q=35.01407,135.75161

                市営12立命館大学前行

                市営9    西賀茂車庫前行

 

         1 站到 堀川丸太町(バス)

                https://maps.google.com.tw/maps?q=35.017276,135.751241

 

        走去對面 堀川丸太町(バス)

                https://maps.google.com.tw/maps?q=35.017406,135.750873

 

Day10 20131015(二) 攝影

  

2013.10.15

 

10:00       出發前往京都駅

         烏丸松原(バス)搭 嵐山本線 京都駅前行      220

         京都駅前(バス)

 

        烏丸松原(バス)往南

                https://maps.google.com.tw/maps?q=34.999747,135.759832

 

10:30

        http://goo.gl/maps/Vpr8f

        https://maps.google.com.tw/maps?q=34.98597,135.75901&ll=34.985713,135.759012&spn=0.000538,0.002642&t=m&brcurrent=3,0x0:0x0,0&z=19&layer=c&cbll=34.985713,135.759012&panoid=kpAnP3BcIioAAAQIt9XxTw&cbp=12,329.44,,0,10.14

        Kimi 陳怡君 080-30255565 公共電話 100

 

Day08 20131013(日) 京都金閣寺二条城 (換房五條烏丸)

2013.10.13

 

京都市巴士一日

http://nicklee.tw/index.php?load=read&id=510

 

08:00 出發 換旅館至 東横INN京都五条烏丸 車程 60 分鐘

 

        步行前往 梅田駅(阪急)搭乘(阪急京都線) 前往 烏丸駅                      390

 

        烏丸 步行至 東横INN京都五条烏丸 寄放行李

                https://maps.google.com.tw/maps?q=34.998636,135.759462

 

09:00       前往 金閣寺 車程 40 分鐘

 

        離開 東横INN京都五条烏丸 前往

        烏丸五条(バス)(往北搭乘 市営101 220 公車 北大路バスターミナル行

                https://maps.google.com.tw/maps?q=34.997370,135.759569

                http://www.city.kyoto.jp/kotsu/busdia/hyperdia/057043.htm

 

Day12 20131017(四) 京都嵐山 (換房梅田中津2)

2013.10.17

 

攜帶 Day1  關西機場 JR西日本の主な駅の『みどりの窓口』買的「嵯峨野トロッコ列車 前売券」

 

寄放行李

東横INN京都五条烏丸 前往 日本京都府五条駅 2 號出口

搭乘 烏丸線 (Karasuma Line)  京都駅                                          210

轉乘 JR 山陰本 (R Sanin Main Line)                                         320

步行  トロッコ亀岡駅 轉乘 嵯峨野トロッコ列車  トロッコ嵐山駅 下車

 

觀光

 

回程至 日本京都府嵐山駅(京福電気鉄道) 搭路面電  日本京都府四条大宮駅        200

 

步行回 東横INN京都五条烏丸

        或到 四条大宮(バス)  嵐山本線 公車 開往京都駅前行 220

        烏丸松原(バス) 下車

 

取行李 步行 前往 烏丸駅 搭乘(阪急京都線) 前往 梅田駅(阪急)                    390

梅田駅(阪急) 步行前往 東横INN梅田中津2

 

Day04 20131009(三) 奈良一日

        最晚 9:00 出發,至少前一天先買好 奈良・斑鳩1dayチケット

 

                奈良・斑鳩1dayチケット (大阪市交通局)                                      1,600

                http://www.kintetsu.co.jp/senden/Railway/Ticket/ikaruga/index.html

                http://nicklee.tw/index.php?load=read&id=706

 

                 快速急行 ,不能搭乘特急列車

                先搭地鐵印上日期後才可搭 バス  生駒ケーブル (生駒纜車)

 

                近鉄大阪難波駅 搭到 近鉄奈良駅 車程 40 分鐘

                快速急行 "不能搭乘特急列車"

 

                近鉄中文網站

                http://www.kintetsu.co.jp/foreign/chinese-han/useful/ticket/3-1.html

 

        大阪難波駅 (近鉄)

                https://maps.google.com.tw/maps?q=34.666877,135.499759

 

        近鉄奈良駅 (近鉄)

                https://maps.google.com.tw/maps?q=日本奈良県奈良市近鉄奈良駅

                https://maps.google.com.tw/maps?q=34.684329,135.827426

 

        到了近鐵奈良車站,馬上可以到旁邊的觀光案內所拿取想要的地圖及觀光資訊