2022年10月7日 星期五

Run powershell with TaskSchd 工作排程器

Reference:
 
在工作排程器執行 powershell,不能直接指定 ps1 檔案。
 
設定方法:
需要在「程式或指令碼」打powershell
「新增引數」打-file “C:\script\import.ps1” 才能正常運行
 

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 "通用的例外處理"
}

LInux samba service on CentOS 7

Reference:
 
安裝:
yum install -y samba

設定:
cp /etc/samba/smb.conf /etc/samba/smb.conf.ori
vi /etc/samba/smb.conf
 
[global]
        workgroup = SAMBA
        security = user
        #passdb backend = tdbsam

        map to guest = bad user
        #printing = cups
        #printcap name = cups
        #load printers = yes
        #cups options = raw

[images]
        comment = public images
        path = /path/to/images
        browseable = Yes
        read only = Yes
        writable = Yes
        guest ok = Yes
        force user = smbuser
 
啟動:
systemctl enable smb.service ; systemctl enable nmb.service
systemctl restart smb.service ; systemctl restart nmb.service
 
設定使用者帳號:
user add smbuser
smbpasswd smbuser
chown -R smbuser /path/to/images