PS函数功能,如何第二次调用

时间:2016-09-07 03:44:09

标签: powershell sccm

我有一个脚本可以兼顾注册表时间值和服务器时间值。我还有一个sccm简单修复脚本。我想要实现的主要是在主脚本中调用两次注册表和服务器时间值。 (之前和之后)和我口吃,因为我无法理解为什么我的compairing脚本第二次没有工作或它显示与第一次运行相同的时间 脚本:

#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 个答案:

答案 0 :(得分: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