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

星期五, 9月 29, 2023

PowerShell網路

最近幫朋友寫腳本,需要變更網路,這首選當然是用 PowerShell ,再來是 netsh 指令。

所以就查了這部份,以下這些腳本都需要管理者權限。

從固定IP改為DHCP

Set-NetIPInterface -InterfaceAlias 'Ethernet 2' -Dhcp Enabled
Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetRoute

從 DHCP 改為固定IP

Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress  IpAddress 192.168.1.10 -PrefixLength 24 -DefaultGateway 192.168.1.1

變更固定IP

Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetRoute
Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetIpAddress
Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress ‑IpAddress 192.168.1.11 -PrefixLength 24 -DefaultGateway 192.168.1.1

重設DNS

Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ResetServerAddresses
Set-DnsClientServerAddress -InterfaceAlias "Wi-fi" -ResetServerAddresses

設定DNS

Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses ("8.8.8.8","8.8.4.4")
Set-DnsClientServerAddress -InterfaceAlias "Wi-fi" -ServerAddresses ("8.8.8.8","8.8.4.4")

參考資料

星期六, 7月 22, 2023

Powershell 找早於15天的檔案

這個需求在 linux 用 find 寫,很方便

find /tmp/location -type f -mtime +15 -delete

在 Windows Powershell 的話,可以參考這篇 Delete files older than 15 days using PowerShell

$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

這是使用 Get-ChildItem -Path $path ,搭配 Where-Object ,Where-Object 裏面是做判斷, $_ 是每個項目,用每個項目的 CreationTime 去判斷,然後用 Remove-Item 去刪除。

星期六, 8月 13, 2022

PowerShell-取得磁碟剩餘空間

朋友主機的作業系統是 Windows Server 2012,之後會定期去做維護,那連線因為懶得用滑鼠按鍵去一個一個看,所以參考網路文章,寫了個簡單的腳本來一次印出來。

這腳本我命名為 du.ps1

function Get-FreeSpace {
    param ([string]$path);
    $space = (Get-Volume -FilePath $path).SizeRemaining;
    return [int64]($space / (1024 * 1024)); # this would otherwise be a float
}

$freeSpace = Get-FreeSpace "C:\"
Write-Host $freeSpace, "MB" -Separator " "
$freeSpace = Get-FreeSpace "D:\"
Write-Host $freeSpace, "MB" -Separator " "
$freeSpace = Get-FreeSpace "E:\"
Write-Host $freeSpace, "MB" -Separator " "
Read-Host "Press ENTER to exit"

簡單說明如下:

  1. Get-Volume -FilePath "<path>" 是取得指定路徑的物件,然後取物件的 SizeRemaining 屬性。
  2. 因為單位是 bytes,所以除以 1024 變成 K,再除以 1024 變成 MB
  3. 用 Write-Host 印出來,因為找不到類似 printf 或 print 的函式。Write-Host 的 -Separator 參數是指定分隔符號,這邊我用 ” ” 當作分隔,所以印出時,就會是 “100 MB” 這樣子的字串。
  4. 最後用 Read-Host 等按鍵,因為之後我會用滑鼠點一點來執行,若不加這行,執行完,視窗就會消失。

開啟 PowerShell 終端機視窗來執行 .\du.ps1 時,會有錯誤訊息,說不允許執行。這時候需要先執行下面這行,才能執行

Set-ExecutionPolicy RemoteSigned

參考文章

星期一, 1月 11, 2010

移除七天前檔案

無心幹正事,可是卻有心搞 script,這真是...


#!/usr/bin/env python
# Usage:
# remove_files_older_than.py ./*.jpg

import sys
import glob
import stat
import os
import time

if len(sys.argv)<2:
print "Need 1 argument!"
sys.exit(-1)

expireDays = 7
fileList = glob.glob( sys.argv[1] )
print "Found %d files." % len(fileList)

currentTime = time.localtime()
deletedCount = 0
for filename in fileList:
filestat = os.stat( filename )
modifiedTime = time.localtime( filestat[ stat.ST_MTIME ] )
diff=time.mktime( currentTime )-time.mktime( modifiedTime )
if (diff/(60.0*60*24))>expireDays :
print diff/(60.0*60*24)
print "Remove %s, mtime=%s..." % (filename, time.strftime(
"%Y/%m/%d %H:%M:%S", modifiedTime ) )
os.remove( filename )
deletedCount=deletedCount+1

if deletedCount > 0:
print "Remove %d files" % deletedCount
else:
print "No files expired."


以下則是摘錄自:Windows Shell 刪除七天前資料
PowerShell的用法

Get-ChildItem -Recurse -force C:\files | Where-Object {!($_.Mode -match "d") -and ((Get-Date).Subtract($_.LastWriteTime).TotalDays -gt 7) } | Remove-Item -Force


Bash 的用法

find -mtime +7 -exec rm -f {} \;

星期一, 7月 20, 2009

無法移除的 Powershell v1.0

如果你也跟我一樣,因為要安裝 Powershell v2.0 而無法移除 Powershell v1.0 的話,可以研讀 Powershell team 貼的這篇:Windows PowerShell Blog : Behind PowerShell Installer (for Windows XP / Windows Server 2003):,了解一下 Powershell 安裝程式做了什麼。不過這篇字太多了,我其實是參考這篇:BUGBUG: poor title » Blog Archive » How to uninstall Powershell v1.0 from Windows Server 2003 R2 來移除的,除了 pwrshsip.dll 無法刪掉之外,基本上不刪掉也沒關係。接著就可以上 Powershell v2.0 CTP3 了。
為什麼要上 Powershell v2.0 CTP3 呢?這是因為 SQL Server 2008 Management Studio Express 的緣故。真是太機車了...