首页 > 解决方案 > 启用/禁用离线文件同步

问题描述

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

在 localhost 上配置离线文件……</h1>

禁用脱机文件失败

禁用离线文件同步

在 localhost 上配置离线文件……</h1>

启用脱机文件失败,需要重新启动

启用离线文件同步

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

这是最初键入的内容:

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” }
}

运行此功能时出现此错误

方法调用失败,因为 [System.Management.ManagementObject] 不包含名为“已启用”的方法。

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

标签: functionpowershellwindows-10

解决方案


这看起来就像我今晚早些时候在 reddit 上看到的同一个问题。我在那里回答了,但我也会在这里给出一个简短的回答。

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

要查看 CIM 类的方法和属性的名称,请使用Get-CimClasscmdlet。

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}

推荐阅读