讓我們瀏覽一下代碼,看看問題出在哪里。 # An array containing service names$Services = @( "mpssvc""wscsvc""clipSVC"#There are more services, but it's not necessary to show them here.) # Loop through the array. Pick a name one by one.foreach ($Service in Services) { # Compare the array contents with service's name. Here's the catch if($Service.Name -NotIn $Services) 那么,怎么了?這是$Service.Name。由于$Service是包含$services集合中當前項的變量,因此它沒有.Name屬性。此外,代碼正在逐個檢查集合是否包含其所有成員。永遠都是這樣。 要實現禁用所需服務,請獲取服務列表,并將其保存到要禁用的服務列表中。這樣,, # An array containing service names$ServicesToDisable = @( "mpssvc""wscsvc""clipSVC")# Get all running services$Ru