function - 启用/禁用离线文件同步
问题描述
这是我找到的网站上的脚本,我想知道是否有办法改进它,因为它运行时我得到了这个:
在 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
解决方案
这看起来就像我今晚早些时候在 reddit 上看到的同一个问题。我在那里回答了,但我也会在这里给出一个简短的回答。
错误是您正在调用方法Enabled
。Enabled
是Win32_OfflineFilesCache
CIM 类的属性名称,表示当前状态。该方法本身被命名为Enable
- 现在时 - 设置状态。命名非常含糊,尤其是因为它还用于禁用脱机文件。
要查看 CIM 类的方法和属性的名称,请使用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}
推荐阅读
- sql - 如何执行从 RAW SQL 到 django 查询集的翻译
- javascript - 在 Acrobat Reader 应用程序版本上隐藏表单域边框
- mysql - Entity Framework Core 中的 PredicateBuilder 与 And & Or (in) 子句 n 相同的查询
- excel - 查找数字系列之间的重叠
- c# - 无法使用 WMI API 在 .NET Framework 中实现 USB 检测
- r - 将 pgm 转换为矩阵
- vuejs2 - 如何通过 laravel 8 惯性中的模型访问关系
- c# - 为什么使用 Selenium C# 清除表单中的字段时,当我点击提交时,我的表单会重新填充数据?
- android - 使用共享首选项更改后记住 TextView 的大小
- python-3.x - Python在读取数据文件时将选项卡识别为字符串