顯示具有 Powershell 標籤的文章。 顯示所有文章
顯示具有 Powershell 標籤的文章。 顯示所有文章

2025年8月5日 星期二

2022年10月7日 星期五

Run powershell with TaskSchd 工作排程器

Reference:
 
在工作排程器執行 powershell,不能直接指定 ps1 檔案。
 
設定方法:
需要在「程式或指令碼」打powershell
「新增引數」打-file “C:\script\import.ps1” 才能正常運行
 
2024/10/23 update:
如果要把output & error 輸出到檔案,「新增引數」打-file “C:\script\import.ps1 *> log.txt”

Reference: 

2022年10月3日 星期一

Powershell Script for Microsoft SQL server

Reference:
 
#連線資料庫
$serverName = "SQL01"
$databaseName = "wiside"
$tableName = "dbo.raw_data"
$uid = 'sa'
$pwd = 'mypassword'

$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$serverName';database='$databaseName';User ID = $uid; Password = $pwd;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection

$insertquery="INSERT INTO $tableName ([ID],[mac],[vendor],[datetime]) VALUES ('$ID','$mac','$vendor','$datetime')"
$Command.CommandText = $insertquery
$Command.ExecuteNonQuery()

$Connection.Close();

catch exception on powershell script

Reference: 

try {
    # 主要程式邏輯
}
catch [System.NotSupportedException] {
    Write-Output "不支援"
}
catch [System.IO.DirectoryNotFoundException] {
    Write-Output "找不到資料夾"
}
catch {
    Write-Output "通用的例外處理"
}

2022年9月23日 星期五

Powershell script 登入網頁、執行指令 (未完成)

Reference:
 
狀況:
有一支IPCAM的NTP有問題,這家廠商已經不提供韌體更新。
登入它的管理網頁有手動同步時間,這功能正常。
我想用powershell script登入IPCAM,再click "time sync"。
但是這一支IPCAM的登入網頁用javascript,我就卡住了.....
 
使用 Powershell ISE
 
$azr = Invoke-WebRequest -Uri "http://211.72.66.73:8120/" -SessionVariable sbv
$azr
$azr|gm
$azr.Forms
$dbform = $azr.Forms["myForm"]
$dbform.Fields
$dbform.Fields["Username"] = "username"
$dbform.Fields["Password"] = "password"
$dbform.Action
$r = Invoke-WebRequest -Uri "http://211.72.66.73:8120/" -WebSession $sbv -Method Post -Body $dbform.Fields