2016-09-07 3 views
0

레지스트리 시간 값과 서버 시간 값을 비교하는 스크립트가 있습니다. 뿐만 아니라 나는 sccm 간단한 수정 스크립트를 가지고있다. 주요 목표는 레지스트리에서 서버 시간 값을 주 스크립트에서 두 번 호출하는 것입니다. (전후) 그리고 난 내가 내 compairing 스크립트가 두 번 작동하지 않는 이유를 이해하지 못할 같이 망가하거나 처음 실행시로 스크립트 같은 시간을 보여주는 :PS 함수에서 함수를 두 번째 호출하는 방법

#SCCM script START 
Function Get-Sccm-Repair-test_01 { 
PARAM(
    [Parameter(Mandatory=$true)] 
    [string]$computer 
) 
############### 
#Start Trigering Application Deployment Evaluation Cycle# 
     Function get-sccm-reg-values { 
param(
    [string]$computer 
    ,[string]$Path= "HKLM\SOFTWARE\Danskebank\Agent Status" 
    ,[string[]]$Properties 
    ,[switch]$Verbose 
) 

if ($Verbose) { $VerbosePreference = 2 } 

    $root, $last = $Path.Split("\") 
    $last = $last[-1] 
    $Path = $Path.Substring($root.Length + 1,$Path.Length - ($last.Length + $root.Length + 2)) 
    $root = $root.TrimEnd(":") 
    switch($root) { 
     "HKCR" { $root = "ClassesRoot"} 
     "HKCU" { $root = "CurrentUser" } 
     "HKLM" { $root = "LocalMachine" } 
     "HKU" { $root = "Users" } 
     "HKPD" { $root = "PerformanceData"} 
     "HKCC" { $root = "CurrentConfig"} 
     "HKDD" { $root = "DynData"} 
     default { return "Path argument is not valid" } 
    } 
    #Access Remote Registry Key using the static OpenRemoteBaseKey method. 
    Write-Verbose "Accessing $root from $computer" 
    $rootkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($root,$computer) 
    if(-not $rootkey) { Write-Error "Can't open the remote $root registry hive" } 

    Write-Verbose "Opening $Path" 
    $key = $rootkey.OpenSubKey($Path) 
    if(-not $key) { Write-Error "Can't open $($root + '\' + $Path) on $computer" } 
    $subkey = $key.OpenSubKey($last) 
    $output = new-object object 

    if($subkey -and $Properties -and $Properties.Count) { 
     foreach($property in $Properties) { 
     Add-Member -InputObject $output -Type NoteProperty -Name $property -Value $subkey.GetValue($property) 
     } 
     Write-Output $output 
     } elseif($subkey) { 
     foreach($property in $subkey.GetValueNames()) 
     { 
     Add-Member -InputObject $output -Type NoteProperty -Name $property -Value $subkey.GetValue($property) 
     } 

} 
     $pcTime = invoke-command -ComputerName $computer -ScriptBlock {Get-Date -DisplayHint Date -Format yyyy-MM-dd} 

     $SCCMValue = $pcTime | Out-String 
     $Rserver = $output.SCCMTimestamp | Out-String 

     if($SCCMValue -eq $Rserver) 
     { 
      Write-Host -foreground "green" "Server curent date:" $pcTime 
      Write-Host -foreground "green" "SCCM Registry Value is up to date:" $output.SCCMTimestamp 
     } 
     else 
     { 
      Write-Host -foreground "red" "Server curent date:" $pcTime 
      Write-Host -foreground "red" "SCCM Registry Value is outdate:" $output.SCCMTimestamp 
     } 
    $key = Out-Null 
    $pcTime = Out-Null 
} 

#END Trigering Application Deployment Evaluation Cycle# 
############### 

     ############### 
     #Start Trigering Application Deployment Evaluation Cycle# 
     $SCCMClient = [wmiclass] "\\$computer\root\ccm:SMS_client" 
     Write-Host -foreground "green" "Application Deployment Evaluation Cycle Updated" 
     $SCCMClient.TriggerSchedule("{00000000-0000-0000-0000-000000000121}") | Out-Null 

     Write-Host -foreground "green" "Machine Policy Retrieval and Evaluation Cycle Updated" 
     $SCCMClient.TriggerSchedule("{00000000-0000-0000-0000-000000000021}") | Out-Null 
     #Stop Trigering Application Deployment Evaluation Cycle# 
     ############### 

     Write-Host -foreground "green" "Gathering data for pending install packages..." 
     $SoftwareApp = Get-WmiObject -Namespace ROOT\ccm\ClientSDK -Class CCM_Application -ComputerName $computer | Select-Object AllowedActions, Fullname | FT -AutoSize 
     Write-Host -foreground "green" "Installing Deployment Test (SCCMTimestamp) package..." 

     #Start Trigering the SCCM package# 
     $AppName = "Deployment Test (SCCMTimestamp)" 

     $s = New-PSSession -ComputerName $Computer 
     Invoke-Command -Session $s -Argu $Computer,$AppName -ScriptBlock ` 
     { 
     param ($Computer,$AppName) 
     write-host "Getting Parameters for '$AppName' on $Computer" 
     $App = Get-WmiObject -computername $Computer -Namespace "root\ccm\ClientSDK" -Class CCM_Application | where {$_.Name -like "$AppName"} | Select-Object Id, Revision, IsMachineTarget 
     $AppID = $App.Id 
     $AppRev = $App.Revision 
     $AppTarget = $App.IsMachineTarget 
     write-host $AppID, $AppRev, $AppTarget -ForegroundColor Yellow 
     write-host "Triggering Installation!" -ForegroundColor Green 
     ([wmiclass]'ROOT\ccm\ClientSdk:CCM_Application').Install($AppID, $AppRev, $AppTarget, 0, 'Normal', $False) | Out-Null 
     } 
     Remove-PSSession $s 
     #End Trigering the SCCM package# 

     #Restarting the service.START 
     Write-Host -foreground "green" "Restarting the HealthService..." 
     $session = New-PSsession -Computername $computer 
     Invoke-Command -Session $Session -ScriptBlock {Restart-Service "HealthService"} | Out-Null 
     Remove-PSSession $Session 
     #Resstarting the service.END 

     ################ Double check the time START 
     Function get-sccm-reg-values { 
param(
    [string]$Path  = "HKLM\SOFTWARE\Danskebank\Agent Status" 
    ,[string[]]$Properties 
    ,[switch]$Verbose 
) 

if ($Verbose) { $VerbosePreference = 2 } 

    $root, $last = $Path.Split("\") 
    $last = $last[-1] 
    $Path = $Path.Substring($root.Length + 1,$Path.Length - ($last.Length + $root.Length + 2)) 
    $root = $root.TrimEnd(":") 
    switch($root) { 
     "HKCR" { $root = "ClassesRoot"} 
     "HKCU" { $root = "CurrentUser" } 
     "HKLM" { $root = "LocalMachine" } 
     "HKU" { $root = "Users" } 
     "HKPD" { $root = "PerformanceData"} 
     "HKCC" { $root = "CurrentConfig"} 
     "HKDD" { $root = "DynData"} 
     default { return "Path argument is not valid" } 
    } 
    #Access Remote Registry Key using the static OpenRemoteBaseKey method. 
    Write-Verbose "Accessing $root from $computer" 
    $rootkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($root,$computer) 
    if(-not $rootkey) { Write-Error "Can't open the remote $root registry hive" } 

    Write-Verbose "Opening $Path" 
    $key = $rootkey.OpenSubKey($Path) 
    if(-not $key) { Write-Error "Can't open $($root + '\' + $Path) on $computer" } 
    $subkey = $key.OpenSubKey($last) 
    $output = new-object object 

    if($subkey -and $Properties -and $Properties.Count) { 
     foreach($property in $Properties) { 
     Add-Member -InputObject $output -Type NoteProperty -Name $property -Value $subkey.GetValue($property) 
     } 
     Write-Output $output 
     } elseif($subkey) { 
     foreach($property in $subkey.GetValueNames()) 
     { 
     Add-Member -InputObject $output -Type NoteProperty -Name $property -Value $subkey.GetValue($property) 
     } 

} 
     $pcTime = invoke-command -ComputerName $computer -ScriptBlock {Get-Date -DisplayHint Date -Format yyyy-MM-dd} 

     $SCCMValue = $pcTime | Out-String 
     $Rserver = $output.SCCMTimestamp | Out-String 

     if($SCCMValue -eq $Rserver) 
     { 
      Write-Host -foreground "green" "Server curent date:" $pcTime 
      Write-Host -foreground "green" "SCCM Registry Value is up to date:" $output.SCCMTimestamp 
     } 
     else 
     { 
      Write-Host -foreground "red" "Server curent date:" $pcTime 
      Write-Host -foreground "red" "SCCM Registry Value is outdate:" $output.SCCMTimestamp 
     } 
} 
     ############### Double check the time END 

     Write-Host -foreground "green" "SCCM fix has been performed, please wait from 5 to 10 minutes untill event close in scom.." 
} 
#SCCM script END 

감사를

답변

1

내가 돈 ' 실제로 함수를 호출하는 위치를 확인하십시오. get-sccm-reg-values ​​함수를 선언하지만 결코 실행하지 마십시오. 그런 다음 나중에 다시 선언하면 첫 번째 선언을 덮어 쓰지 만 다시 실제로는 호출하지 않습니다. 두 함수를 모두 선언하고 다른 함수를 호출합니다. 예 :

#Create a function 
function foo{ 
param() 
    return Get-Date 
} 
#create second function 
function bar{ 
param() 
    #functions have to be called to do anything 
    foo 
    Start-Sleep -Seconds 2 
    #call the same function again 
    foo 
} 

#Call the second function 
bar 
관련 문제