Powershell 正在尝试编写一个自动时区更新脚本
我正在编写powershell脚本,但它没有正确读取我的“If,elseIf”,因此我知道我做错了什么。我需要一些人帮我弄清楚这件事。 我首先拉取默认网关并存储它:Powershell 正在尝试编写一个自动时区更新脚本,powershell,timezone,ip,powershell-4.0,ntp,Powershell,Timezone,Ip,Powershell 4.0,Ntp,我正在编写powershell脚本,但它没有正确读取我的“If,elseIf”,因此我知道我做错了什么。我需要一些人帮我弄清楚这件事。 我首先拉取默认网关并存储它: $Gateway = (Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway 接下来,我试图让它排序,这样,如果它等于我指定的默认网关之一,它将更新时区 If ($Gateway = "10.100.4.1") {
$Gateway = (Get-wmiObject Win32_networkAdapterConfiguration | ?{$_.IPEnabled}).DefaultIPGateway
接下来,我试图让它排序,这样,如果它等于我指定的默认网关之一,它将更新时区
If ($Gateway = "10.100.4.1")
{
$TZ = "Central Standard Time"
}
ElseIf ($Gateway = "10.101.4.1")
{
$TZ = "Central Standard Time"
}
我正在用一个
Set-TimeZone $TZ
其目的是,如果我在家庭办公室对系统进行镜像,并将其发送到“远程位置”,我就不能相信最终用户会更新他们的时区,而且我的POS写得很差,因此它不使用UTC/GMT,并且可能会导致BOH系统等问题
我将把它作为一个启动项,在系统启动时执行,以确保它始终与TZ保持最新
将Win 10更改为对TZ使用自动更新是不起作用的,因为原因是(阅读:网络团队和安全团队想要得到我,在这种情况下,这不是妄想)
那么,我在哪里可以找到帮助来把这些放在一起呢?
编辑:我有一个打字错误,这就是为什么它不起作用。所以没有关系。对于那个些对打字错误感兴趣的人,我已经把它删除了。它位于我的$Gateway部分,在{$.IPEnabled}之后和之前添加了一个“您的“if”语句使用赋值运算符=
而不是相等运算符-eq
将其切换到If($Gateway-eq“10.100.4.1”)
应该可以工作
另外,我错过了您提到的输入错误,但赋值运算符仍然是一个问题。当赋值运算符用于“if/elsif”语句时,它将始终返回
$true
,这将是一个很大的问题。我建议对指定的默认网关使用查找哈希表。因为您希望将其作为启动脚本运行,所以还需要创建一个集中的路径,在该路径中可以记录错误或在windows事件日志中写入错误 创建一个哈希表,指定网关IP地址作为键,时区ID作为值 直接在代码中:
$timeZones = @{
'10.100.4.1' = 'Central Standard Time'
'10.101.4.1' = 'Central Standard Time'
'10.102.4.1' = 'Eastern Standard Time'
# etc.
}
或者通过读取列为“IPAddress”和“TimeZone”的集中式CSV文件
$defaultTz = Import-Csv -LiteralPath '\\Server\Share\Folder\Timezones.csv'
$timeZones = @{}
$defaultTz | ForEach-Object {
$timeZones[$_.IPAddress] = $_.TimeZone
}
接下来,使用如下值(演示使用一个集中的错误日志文件):
我应该澄清一下,我在美国有大约50个这样的站点,并且默认网关都链接到不同的时区。研究一下使用哈希表进行查找。使用IP字符串作为键,使用TZ名称作为值。这将加快查找速度,并使维护GW/TZ地图列表更容易。此外,您的
$Gateway
看起来很简单ike它将是一个数组,而不是单个IP。您正在使用的调用通常返回一个数组,第一个数组是ipv4,第二个数组是ipv6值。由于是新的,因此您可能不知道这一点,但通常通过单击✓ 这将帮助其他有类似问题的人更容易找到它。
$errorlog = '\\Server\Share\Folder\TimezonesErrors.log'
$now = (Get-Date).ToString() # use default format or specify the date format yourself here
$currentTz = (Get-TimeZone).Id # get the current timezone Id
# get the default gateway IPv4 address for this computer
$gw = @((Get-wmiObject Win32_networkAdapterConfiguration | Where-Object {$_.IPEnabled -and $_.DefaultIPGateway-like '*.*.*.*'}).DefaultIPGateway)[0]
# check if the gateway IP address is present in the lookup Hashtable
if ($timeZones.ContainsKey($gw)) {
$tz = $timeZones[$gw]
# only set the wanted timezone if it differs from the current timezone
if ($tz -ne $currentTz) {
try {
Set-TimeZone -Id $tz -ErrorAction Stop
}
catch {
# add the exception to the error log
$msg = "$now - Error setting the timezone on computer $($env:COMPUTERNAME): $_.Exception.Message"
Add-Content -LiteralPath $errorlog -Value $msg
}
}
}
else {
# make it known that the IP address for this gateway was not found in the Hashtable
$msg = "$now - No timezone found for default gateway $gw on computer $($env:COMPUTERNAME)"
# write error to the central error.log file
Add-Content -LiteralPath $errorlog -Value $msg
}