Enable/Disable 离线文件同步

Enable/Disable Offline File Sync

这是我从网站上找到的脚本,我想知道是否有改进它的方法,因为它是 运行ning 我得到这个:

在本地主机上配置离线文件…

禁用离线文件失败

在本地主机上配置离线文件…

启用脱机文件失败,原因是 需要重启

我的意思是它做了它应该做的事情,只是那个消息很烦人

这是最初键入的内容:

param($computer=”localhost”, $a, $help)


function funline ($strIN)
{
 $num = $strIN.length
 for($i=1 ; $i -le $num ; $i++)
  { $funline += “=” }
    Write-Host -ForegroundColor yellow $strIN 
    Write-Host -ForegroundColor darkYellow $funline
}

function funHelp()
{
$helpText=@”
DESCRIPTION:
NAME: EnableDisableOffLineFiles.ps1 
Enables or disables offline files on a local or remote machine.
A reboot of the machine MAY be required. This information will
be displayed in the status message once the script is run.

PARAMETERS: 
-computer Specifies name of the computer upon which to run the script
-a(ction) < e(nable), d(isable) >
-help     prints help file

SYNTAX:
EnableDisableOffLineFiles.ps1 -computer MunichServer -a e

Enables offline files on a computer named MunichServer

EnableDisableOffLineFiles.ps1 -a d

Disables offline files on local computer

EnableDisableOffLineFiles.ps1 -help ?

Displays the help topic for the script

“@
$helpText
exit
}

function funtranslatemethod($a)
{
 switch($a)
  {
   “e” { $global:m = $true 
         $global:msg = “Enable offline files” 
       }
   “d” { 
        $global:m = $false
        $global:msg = “Disable offline files” 
       }
  default{ 
          $global:msg = “$a is not an allowed response`n” 
 }
  }
}

if($help){ funline(“Obtaining help …”) ; funhelp }
if(!$a)
   {
    $(throw “You must supply an action. try this:
EnableDIsableOfflineFiles.ps1 -help ?”)
   }
$global:msg =$global:m = $null
funtranslatemethod($a)

$objWMI = [wmiclass]”\$computer\root\cimv2:win32_offlinefilescache”
funline(“Configure Offline files on $computer …”)
$rtn = $objwmi.enable($m)
if($rtn.returnvalue -eq 0)
 {
  Write-Host -ForegroundColor green “$msg succeeded”
 }
ELSE
 {
  Write-Host -ForegroundColor red “$msg failed with $($rtn.returnvalue) ”
 }
if($rtn.rebootrequired) 
  { Write-Host -ForegroundColor cyan “reboot required” }

这是我做的

Function EnableDisable-OfflineFiles{
param($computer=”localhost”, $a, $help)

<# 
DESCRIPTION:
NAME: EnableDisableOffLineFiles.ps1 
Enables or disables offline files on a local or remote machine.
A reboot of the machine MAY be required. This information will
be displayed in the status message once the script is run.

PARAMETERS: 
-computer Specifies name of the computer upon which to run the script
-a(ction) < e(nable), d(isable) >
-help     prints help file

SYNTAX:
EnableDisableOffLineFiles.ps1 -computer MunichServer -a e

Enables offline files on a computer named MunichServer

EnableDisableOffLineFiles.ps1 -a d

Disables offline files on local computer

EnableDisableOffLineFiles.ps1 -help ?

Displays the help topic for the script

“@
$helpText
#>
function funline ($strIN)
{
 $num = $strIN.length
 for($i=1 ; $i -le $num ; $i++)
  { $funline += “=” }
    Write-Host -ForegroundColor yellow $strIN 
    Write-Host -ForegroundColor darkYellow $funline
}

function funHelp()
{
$helpText=@”
DESCRIPTION:
NAME: EnableDisableOffLineFiles.ps1 
Enables or disables offline files on a local or remote machine.
A reboot of the machine MAY be required. This information will
be displayed in the status message once the script is run.

PARAMETERS: 
-computer Specifies name of the computer upon which to run the script
-a(ction) < e(nable), d(isable) >
-help     prints help file

SYNTAX:
EnableDisableOffLineFiles.ps1 -computer MunichServer -a e

Enables offline files on a computer named MunichServer

EnableDisableOffLineFiles.ps1 -a d

Disables offline files on local computer

EnableDisableOffLineFiles.ps1 -help ?

Displays the help topic for the script

“@
$helpText

}

function funtranslatemethod($a)
{
 switch($a)
  {
   “e” { $global:m = $true 
         $global:msg = “Enable offline files” 
       }
   “d” { 
        $global:m = $false
        $global:msg = “Disable offline files” 
       }
  default{ 
          $global:msg = “$a is not an allowed response`n” 
 }
  }
}

if($help){ funline(“Obtaining help …”) ; funhelp }
if(!$a)
   {
    $(throw “You must supply an action. try this:
EnableDIsableOfflineFiles.ps1 -help ?”)
   }
$global:msg =$global:m = $null
funtranslatemethod($a)

$objWMI = Get-WmiObject -Class Win32_OfflineFilesCache -Computer $computer
funline(“Configure Offline files on $computer …”)
$rtn = $objwmi.Enabled($m)
if($rtn.returnvalue -eq 0)
 {
  Write-Host -ForegroundColor green “$msg succeeded”
 }
ELSE
 {
  Write-Host -ForegroundColor red “$msg failed with $($rtn.returnvalue) ”
 }
if($rtn.rebootrequired) 
  { Write-Host -ForegroundColor cyan “reboot required” }
}

我在 运行 这个函数

时遇到这个错误

Method invocation failed because [System.Management.ManagementObject] does not contain a method named 'Enabled'.

  • $rtn = $objwmi.Enabled($m)
  •   + CategoryInfo          : InvalidOperation: (Enabled:String) [], RuntimeException
      + FullyQualifiedErrorId : MethodNotFound
    

失败的行是:

$rtn = $objwmi.Enabled($m)

您的首字母 'E' 已启用。 当你像在原始脚本中那样将那个小写字母时会发生什么。

这看起来像 the same question 我今晚早些时候在 reddit 上看到的。我在那边回答了,但我也会在这里做一个简短的回答。

错误是您正在调用方法 EnabledEnabledWin32_OfflineFilesCache CIM class 的 属性 的名称,表示当前状态。该方法本身被命名为 Enable - 现在时 - 用于设置状态。命名非常含糊,尤其是因为它还用于禁用 离线文件。

要查看 CIM class 的方法和属性的名称,请使用 Get-CimClass cmdlet。

Get-CimClass -ClassName Win32_OfflineFilesCache | Select-Object -ExpandProperty CimClassMethods

Name              ReturnType Parameters                               Qualifiers           
----              ---------- ----------                               ----------           
Enable                UInt32 {Enable, RebootRequired}                 {implemented, static}
RenameItem            UInt32 {NewPath, OriginalPath, ReplaceIfExists} {implemented, static}
RenameItemEx          UInt32 {NewPath, OriginalPath, ReplaceIfExists} {implemented, static}
Synchronize           UInt32 {Flags, Paths}                           {implemented, static}
Pin                   UInt32 {Deep, Flags, Paths}                     {implemented, static}
Unpin                 UInt32 {Deep, Flags, Paths}                     {implemented, static}
DeleteItems           UInt32 {Flags, Paths}                           {implemented, static}
Encrypt               UInt32 {Encrypt, Flags}                         {implemented, static}
SuspendRoot           UInt32 {Path, Suspend}                          {implemented, static}
TransitionOffline     UInt32 {Flags, Force, Path, OpenFiles}          {implemented, static}
TransitionOnline      UInt32 {Flags, Path}                            {implemented, static}