Jump to content

File and printer sharing for Microsoft networks


 Share

Recommended Posts

Hi,

At work we have to set up PC's a specific way, and to do that I've written an AutoIT script to automate it.  Bearing in mind we have 2000+ users, it saves us alot of time.

Anyway, one of the things we need to do is to enable "File and Printer sharing for Microsoft Networks", so that we can do various things remotely.  The part of the script that does this, works fine.  Or at least I thought it did.  It used to use a command that would uninstall and reinstall the "File and Printer Sharing for MS Networks" (can't remember the command now) but that stopped working (at least here) in Windows 10.  So I just wrote a very crude script that just automates the mouse clicks and key presses.  It checks to see if the box is ticked by looking at the pixel colour at the center of the box.  If it's ticked already, leave it alone, if it's not, tick it.  Very basic, and there's probably a better way of writing it, I'm sure, but I understand it and it works.

At least I thought it did,  On desktops, it works perfectly, but I tried it on a laptop and for some reason, pixel colour is not the same as what it is on a desktop.  Here's the script:

 

#RequireAdmin

AutoItSetOption( "MouseCoordMode",0 )
AutoItSetOption( "PixelCoordMode",0 )
Run ('control ncpa.cpl')
WinWait ( "Network Connections")
SEND ("{TAB 6}")                        ; Tab to Ethernet icon
Sleep ( 500 )
SEND ("{DOWN}")                         ; Highlight the Ethernet icon
Sleep ( 500 )
SEND ("+{F10}")                         ; Right-click it
Sleep ( 500 )
SEND ("{DOWN 8}")                       ; Cursor down to Properties
Sleep ( 500 )
SEND("{ENTER}")                         ; Click Properties
Sleep ( 2000 )
SEND("{DOWN}")                          ; Cursor down to File and Printer Sharing for Microsoft Networks
sleep ( 500 )
Local $iColor = PixelGetColor(42, 195)  ; Get the colour of a pixel in the checkbox
If $iColor = 16777215 Then              ; If the colour is white the box isn't ticked, so carry on and tick it
    SEND("{SPACE}")
    Sleep ( 1000 )
    SEND("{TAB 3}")
    Sleep ( 500 )
    SEND("{SPACE}")
    Sleep ( 500 )
    Send ( "!{F4}")
    Sleep(2000)
Else
    Send ( "!{F4}")                     ; If the colour isn't white, then the box is already ticked, so ignore it and move on
    Sleep ( 500 )
    Send ( "!{F4}")
    Sleep(2000)
    EndIf

What have I done wrong?  How can it work on a desktop PC and not a laptop?  Is there a completely (and better way) to do what I'm trying to do above?

Thanks in advance.

Gray

 

 

Link to comment
Share on other sites

Your approach is completely random.  Each computer has its own configuration. Each OS has its specificity. Trying to get things done with keys and colors are doom to fail. You need to get this at the level of the Windows API.  Whether you want to use WMi or other means, you need to figure out what you want to achieve, and script it with those tools.  I know it is a huge change of programming strategy, but believe me it will pay off at the end.

Link to comment
Share on other sites

Here is a function that I use to enable file and print sharing on Windows 7 and Windows 10 PC.  

#RequireAdmin
#include <Constants.au3>
#include <WinAPIFiles.au3>
Global Const $sSystemDir = @WindowsDir & "\System32" ;@SystemDir returns @WindowsDir & "\SYSWOW64" with a 32 bit script.

Func _EnableFileAndPrintSharing() ;Enable File and Print Sharing.
    
    ;Disable x86 redirection mechanism for a 32-bit script.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False) 
    
    Local $iPIDNetsh = Run('netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes', $sSystemDir, @SW_HIDE, $STDERR_MERGED) ;Enable File and Print Sharing
    
    ;Enable x86 redirection mechanism for a 32-bit script.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True) 
    
    ProcessWaitClose($iPIDNetsh)
    Local $sNetshOutput = StringStripWS(StdoutRead($iPIDNetsh), $STR_STRIPLEADING + $STR_STRIPTRAILING)

    If StringRegExp($sNetshOutput, "Updated \d+ rule\(s\)\.\s*Ok\.") Then Return True

    Return SetError(1, 0, False)
EndFunc   ;==>_EnableFileAndPrintSharing

 

Adam

 

Edited by AdamUL
Updated missing includes and constant.
Link to comment
Share on other sites

9 hours ago, Nine said:

Your approach is completely random.  Each computer has its own configuration. Each OS has its specificity. Trying to get things done with keys and colors are doom to fail. You need to get this at the level of the Windows API.  Whether you want to use WMi or other means, you need to figure out what you want to achieve, and script it with those tools.  I know it is a huge change of programming strategy, but believe me it will pay off at the end.

It might be random, but my method has worked for many years that I've worked there.  We only use Dells, and the Windows are configured identically across all hardware.  I really don't have the time to learn other "programming strategies" unfortunately.  I'm not a programmer, and I have to support over 120 different systems to keep a huge production plant running.

Link to comment
Share on other sites

9 hours ago, AdamUL said:

Here is a function that I use to enable file and print sharing on Windows 7 and Windows 10 PC.  

Func _EnableFileAndPrintSharing() ;Enable File and Print Sharing.
    
    ;Disable x86 redirection mechanism for a 32-bit script.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False) 
    
    Local $iPIDNetsh = Run('netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes', $sSystemDir, @SW_HIDE, $STDERR_MERGED) ;Enable File and Print Sharing
    
    ;Enable x86 redirection mechanism for a 32-bit script.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True) 
    
    ProcessWaitClose($iPIDNetsh)
    Local $sNetshOutput = StringStripWS(StdoutRead($iPIDNetsh), $STR_STRIPLEADING + $STR_STRIPTRAILING)

    If StringRegExp($sNetshOutput, "Updated \d+ rule\(s\)\.\s*Ok\.") Then Return True

    Return SetError(1, 0, False)
EndFunc   ;==>_EnableFileAndPrintSharing

 

Adam

 

I couldn't get that script to work, it came up with all kinds of undefined errors.  I'm pretty sure the "netsh" command was the one that no longer works on our PC's though.

And will that script actually tick the box next to "File and printer sharing for Microsoft networks" in the Ethernet settings?  That's what I need to tick, what we do doesn't work without it.

Link to comment
Share on other sites

The function does not automate the GUI ("tick the box"), it uses a command to do the settings.
Can you please post the script you run and the error messages you get?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

5 minutes ago, water said:

The function does not automate the GUI ("tick the box"), it uses a command to do the settings.
Can you please post the script you run and the error messages you get?

I literally just ran the function shown, I'm no expert with AutoIT so I just expected that to be enough :(

Link to comment
Share on other sites

You need to add some include files and define the undefined variable. I added the corresponding lines:

; #RequireAdmin ; <== Maybe you need to activate this statement
#include <Array.au3>
#include <WinAPIFiles.au3>

_EnableFileAndPrintSharing()
Exit

Func _EnableFileAndPrintSharing() ;Enable File and Print Sharing.

    Local $sSystemDir = @SystemDir ; <== Added as $sSystemDir is undefined

    ;Disable x86 redirection mechanism for a 32-bit script.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)

    Local $iPIDNetsh = Run('netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes', $sSystemDir, @SW_HIDE, $STDERR_MERGED) ;Enable File and Print Sharing

    ;Enable x86 redirection mechanism for a 32-bit script.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True)

    ProcessWaitClose($iPIDNetsh)
    Local $sNetshOutput = StringStripWS(StdoutRead($iPIDNetsh), $STR_STRIPLEADING + $STR_STRIPTRAILING)

    If StringRegExp($sNetshOutput, "Updated \d+ rule\(s\)\.\s*Ok\.") Then Return True

    Return SetError(1, 0, False)
EndFunc   ;==>_EnableFileAndPrintSharing

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Excellent thank you.  I've ran it, and the script runs now, but it's still not clicking the box.  I'm at home now so I can't test this at work, but I'm pretty sure in the tests I've done, unl;ess that box is ticked, copying files etc. across our network just doesn't work.

Edited by graybags
Link to comment
Share on other sites

Let's add some error checking (untested):

; #RequireAdmin ; <== Maybe you need to activate this statement
#include <Array.au3>
#include <WinAPIFiles.au3>

Local $bRuleSet = _EnableFileAndPrintSharing()
MsgBox(0, "Result", "Return value: " & $bRuleSet & @CRLF & "@error: " & @error)
Exit

; @error = 1: Update of the rule didn't work
; @error = 2: _WinAPI_Wow64EnableWow64FsRedirection(False) didn't work
; @error = 3: _WinAPI_Wow64EnableWow64FsRedirection(True) didn't work
Func _EnableFileAndPrintSharing() ;Enable File and Print Sharing.

    Local $sSystemDir = @SystemDir ; <== Added as $sSystemDir is undefined
    Local $bResult

    ;Disable x86 redirection mechanism for a 32-bit script.
    If @OSArch = "X64" And Not @AutoItX64 Then
        If _WinAPI_Wow64EnableWow64FsRedirection(False) = False Then Return SetError(2, 0, False)
    Endif

    Local $iPIDNetsh = Run('netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes', $sSystemDir, @SW_HIDE, $STDERR_MERGED) ;Enable File and Print Sharing

    ;Enable x86 redirection mechanism for a 32-bit script.
    If @OSArch = "X64" And Not @AutoItX64 Then
        If _WinAPI_Wow64EnableWow64FsRedirection(True) = False Then Return SetError(3, 0, False)
    Endif
    ProcessWaitClose($iPIDNetsh)
    Local $sNetshOutput = StringStripWS(StdoutRead($iPIDNetsh), $STR_STRIPLEADING + $STR_STRIPTRAILING)

    If StringRegExp($sNetshOutput, "Updated \d+ rule\(s\)\.\s*Ok\.") Then Return True

    Return SetError(1, 0, False)
EndFunc   ;==>_EnableFileAndPrintSharing

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

BTW: When posting please do not click on the quote button (we know what we have written ;) ) but simply add your text at the end of the thread.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Could you please open a cmd prompt and run

Quote

netsh advfirewall firewall show rule "File and Printer Sharing"

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

You could list all rules, pipe them to a file and then check for the correct rule:

Quote

netsh advfirewall firewall show rule name=all > C:\temp\rules.txt

When I list all rules I get more than one for "File and Printer Sharing" (my system is german so I get "Datei- und Druckerfreigabe")

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...