2010-02-11 3 views
2

PowerShell에서 스크립트 실행 동작합니다. 연결되어 있으면 무선 네트워크 카드를 비활성화하십시오. 온보드 네트워크 카드가 연결되어 있지 않으면 무선 카드를 다시 활성화하십시오.는 서비스가 이상하게</p> <p>동작하는 프로젝트로 이상하게

이유 : 사용자가 항상 핫 도킹하여 펑키 한 라우팅 테이블을 얻거나 잘못된 DNS 서버에 바인딩됩니다. 그들이 로컬 리소스에 액세스하려고 할 때, 프린터라고 말하면, 그들은 큐브에있을 수 없으며 큐브에 있습니다 (티켓을 작성하지만 로컬 리소스도됩니다). 사용자가 자신의 무선 기능을 사용하지 않도록 설득하려고 시도하는 경우 (노트북의 스위치를 통해) 또는 핫 도크가 아닌 경우 성공을 거두지 못했습니다.

문제점 : 아래의 PowerShell 스크립트가 실행되고 테스트 조건에서 작동합니다. 대부분의 테스트 조건에서 코드 및 wmi 쿼리가 꽤 일반적 일 수 있습니다. 스크립트를 수동으로 실행하면 예상 결과가 나오지만, 스크립트를 srvany.exe에서 찾을 수있는 유일한 방법으로 서비스를 실행하면 예기치 않은 결과가 발생하고 "문제가 발생했습니다".

세부 정보 : 스크립트를 서비스로 실행하면 srvany.exe를 통해 ONCE가 작동합니다. 네트워크 연결을 테스트하기 위해 루프가 돌아 왔을 때 또는이를 사용 가능하게하거나 사용 불가능하게 할 때 메소드가 시도합니다. 오류는 "get-wmiobject"가 적절한 Cmdlet이 아님을 나타냅니다. 응? 수동으로 작동하지만 한 번 작동하지만 두 번째로 무선 네트워크 카드를 비활성화 한 후에는 작동하지 않습니다. 더 나쁜 아직 내 껍질, 서비스의 바깥, 갑자기 때까지 - wmiobject, 할 수 없어 .... 장치 관리자로 이동하여 무선 네트워크 카드를 다시 활성화 할 때까지

디버깅 시도 : 스크립트를 다시 작성하고 Do While 루프 외부에서 개체를 가져올 수 있도록 약간 정리했습니다. 변경된 사항은 없지만 필자는 어쨌든 스크립트를 그대로 유지했습니다. 나는 "데스크탑과 인터랙트"를 서비스 속성에서 사용할 수 있었고 스크립트가 제대로 작동하는지 확인하고 앞에서 언급 한 오류를 볼 수있다. 도와주세요. 여기서도 무선 네트워크 카드를 비활성화하고 활성화하기 위해 Vista 또는 7에서 충분한 권한을 가진 백그라운드 프로세스를 실행합니다.

#*********************************************************************** 
# "switch-wifi-srv.ps1" 
# This script attempts to identify if a wired network card is in use if 
# one is, the Wireless network card is disabled, until the wired network 
# card is no longer in use. 
# 
# Written by Aaron Wurthmann - aaron (AT) wurthmann (DOT) com 
# 
# 2010.02.10 ver 2 (Service Version) 
# If you edit please keep my name or at the very least original author's. 
# As of this writing I am unsure if script will work with all scenarios, 
# however it has worked for me on Dell laptops running Windows 7 x64. 
# ----------------------------------------------------------------------- 
# This script comes with ABSOLUTELY NO WARRANTY. 
# You may redistribute copies of the script under 
# the terms of the GNU General Public License. 
# ----------------------------------------------------------------------- 
# Service Installation: 
# Aquire and install the Windows 2003 Resource Kit OR the srvany.exe. 
# Use sc.exe and srvany.exe to create a service.... 
# sc create SwitchWifiAuto binPath= "C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe" DisplayName= "Switch Wifi Automatically" 
# Edit registry entry for SwitchWifiAuto, add a key and a string value... 
# HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SwitchWifiAuto\Parameters] 
# "Application"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy RemoteSigned -File C:\\SwitchWifiAuto\\switch-wifi-srv.ps1" 
#************************************************************************ 


$state="" 
$wireStatus="" 
$wifiStatus="" 

# Get Wired and Wireless Card Objects 
$objWire=get-wmiobject -class win32_networkadapter -namespace root\CIMV2 | Where-Object {$_.Name -notmatch "Wireless" -and $_.Name -notmatch "Virtual" -and $_.PhysicalAdapter -eq "True"} 
$objWifi=get-wmiobject -class win32_networkadapter -namespace root\CIMV2 | where-object {$_.Name -match "Wireless"} 

# Get Name of Service to be Used in totally useless Do While Loop 
$objService=get-service -display "Switch Wifi Automatically" 

# Begin Do While Loop 
Do { 
# Get status of wired network card. If enabled and connected set $state to Disable (which will later Disable the Wifi network card) 
    [string]$wireStatus=$objWire | % {$_.NetEnabled} 
    if($wireStatus -eq "True") { 
     $state="Disable" 
    } 
# Get Status of wireless card. 
    if($objWifi){ 
     [string]$wifiStatus=$objWifi | % {$_.NetEnabled} 
#  If $state is not set to disable and if the wireless card is currently disabled, enable it. 
     if($state -ne "Disable") { 
      if($wifiStatus -eq "False") { 
       Out-Null -InputOject ($objWifi | % {$_.Enable()}) 
      } 
#  If $state is set to Disable and if wireless card is currently enabled, disable it. 
     } else { 
      if($wifiStatus -eq "True") { 
       Out-Null -InputOject ($objWifi | % {$_.Disable()}) 
      } 
     } 
    } 

# Reset Checked Variables for good measure 
$state="" 
$wireStatus="" 
$wifiStatus="" 

# Sleep for 120 seconds (two minutes) 
Start-Sleep -s 120 

# Continuing looping (do while) until the service is not running. 
# This is of course technically useless as when the service is not running neither is the script to check if the service is not running. 
# I made it this way however because I don't like infinite loops and I thought it would be funny to it this way instead of while $var=0 
} while ($objService.Status -eq "Running") 

답변

1

출력물을 제거하십시오. 서비스에 stdout 스트림이 없습니다. 그리고 버퍼가 가득 차면 이상한 일이 일어납니다. 그냥 짐작 (나는 결코 파워 쉘을 사용하지 않았다).

+0

아주 좋은 지적입니다. 현재 실제로 결과가 나오고 있습니다. 비활성화 또는 활성화하는 방법이 발생하면 출력이 나타납니다. Hrmmmm –

+0

그게 도움이되지는 않았지만 출력을 억제하는 방법을 배우고 해결할 이유가 생겼습니다. 변경 사항을 반영하기 위해 원본 게시물을 편집 중입니다. –

0

디버깅 시도 : 스크립트를 다시 작성하고 으로 약간 정리하여 Do While 루프 외부에서 개체를 가져올 수 있습니다.

루프 내에 이들을 포함해야합니다. 그렇지 않으면 업데이트 된 값을 얻지 못하고 루프는 아무 것도하지 않습니다.